CN111309671A - Method, device and storage medium for exporting PDF (Portable document Format) of web report - Google Patents

Method, device and storage medium for exporting PDF (Portable document Format) of web report Download PDF

Info

Publication number
CN111309671A
CN111309671A CN201911227222.6A CN201911227222A CN111309671A CN 111309671 A CN111309671 A CN 111309671A CN 201911227222 A CN201911227222 A CN 201911227222A CN 111309671 A CN111309671 A CN 111309671A
Authority
CN
China
Prior art keywords
pdf
dom node
exporting
height
dom
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN201911227222.6A
Other languages
Chinese (zh)
Other versions
CN111309671B (en
Inventor
温桂龙
刘玉明
韦方宇
彭玉冰
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhuhai Pilot Technology Co ltd
Original Assignee
Zhuhai Pilot Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhuhai Pilot Technology Co ltd filed Critical Zhuhai Pilot Technology Co ltd
Priority to CN201911227222.6A priority Critical patent/CN111309671B/en
Publication of CN111309671A publication Critical patent/CN111309671A/en
Application granted granted Critical
Publication of CN111309671B publication Critical patent/CN111309671B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/10File systems; File servers
    • G06F16/11File system administration, e.g. details of archiving or snapshots
    • G06F16/116Details of conversion of file system types or formats
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/95Retrieval from the web
    • G06F16/958Organisation or management of web site content, e.g. publishing, maintaining pages or automatic linking
    • G06F16/986Document structures and storage, e.g. HTML extensions

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Information Transfer Between Computers (AREA)
  • Document Processing Apparatus (AREA)

Abstract

The invention discloses a method for exporting PDF (Portable document Format) from a web report, which comprises the steps of firstly setting a custom attribute according to business requirements, then inserting the corresponding custom attribute into an HTML (hypertext markup language) tag during exporting, then dynamically calculating the actual height of a PDF page occupied by a DOM (document object model) node in the HTML tag corresponding to the report after rendering during exporting, and finally calling a PDF plug-in to convert picture data converted from canvas into a PDF file to export, thereby greatly improving the efficiency of exporting PDF from the web report. The invention also provides a device for exporting the PDF from the web report and a storage medium.

Description

Method, device and storage medium for exporting PDF (Portable document Format) of web report
Technical Field
The invention relates to a business system, in particular to a method, a device and a storage medium for exporting PDF from a web report.
Background
The company business system generally has a report function for performing statistics of data. Usually, the reports are displayed on a Web (World Wide Web, Web page), when the reports are exported and printed into a written form, due to the graphs and tables in the reports and even the tree-shaped mechanism, errors are easily generated when the reports are exported, and finally the printed reports are generated with errors, usually manual adjustment is needed or the graphs and tables are drawn in a file again, so that the report export efficiency is low, and the working efficiency of business personnel is low.
Disclosure of Invention
In order to overcome the defects of the prior art, one of the purposes of the present invention is to provide a method for exporting a PDF web report, which can solve the problems of low exporting efficiency and the like caused by manual operation required for exporting a report in a web page in the prior art.
The invention also aims to provide a device for exporting PDF from a web report, which can solve the problems that the export of the report in a web page in the prior art needs manual operation, so that the export efficiency is low and the like.
The invention also aims to provide a storage medium which can solve the problems that the exporting of the report form in the web page in the prior art needs manual operation, so that the exporting efficiency is low and the like.
One of the purposes of the invention is realized by adopting the following technical scheme:
a method of web reporting export PDF, the method comprising:
and an attribute definition step: setting a custom attribute according to the service requirement, wherein the custom attribute is shown in the following table;
Figure BDA0002302578050000021
and an attribute inserting step: when an export instruction is received, inserting custom attributes into corresponding tags in an HTML document corresponding to a report according to business requirements, and assigning values to each custom attribute;
cloning step: acquiring a DOM node corresponding to a DIV tag from an HTML document according to the ID value of the DIV tag in the HTML document corresponding to the report, creating a cloned Dom node according to the DOM node, assigning the contents of all HTML tags in the DOM node to the cloned Dom node and setting the position of the cloned Dom node in a browser;
a first calculation step: calculating the height of the PDF page occupied by the clone Dom node during exporting according to the size of the standard A4 paper and the width of the PDF page occupied by the clone Dom node during exporting; the size of the PDF page is the size of a standard A4 paper; the width of the PDF page occupied by the cloned Dom node during the exporting process is obtained from a DIV label corresponding to the cloned Dom node;
a second calculation step: obtaining Dom child nodes corresponding to all DIV labels with custom attribute name being paper according to a clone Dom node, and then respectively calculating the height of PDF page occupied by the corresponding Dom child node during exporting according to the custom attributes marginop, marginbottom and page of the DIV label corresponding to each Dom child node and the height of the clone Dom node during exporting;
an adding step: adding the clone Dom node to a Body tag of an HTML document of the browser;
canvas creation step: setting and creating a canvas Dom node according to the width and the height of the DOM node and the canvas scaling ratio, and keeping the position of the canvas Dom node in the browser consistent with the position of the cloned Dom node in the browser;
and (3) a derivation step: and converting the contents of all HTML tags of the clone Dom node into picture data in the canvas Dom node through an HTML2canvas function, and calling a PDF plug-in to export the picture data from the browser into a PDF file.
Further, the deriving step further comprises:
when the height of the picture data is smaller than or equal to the height of the PDF page, exporting the picture data from a browser into a PDF file by calling a PDF plug-in;
when the height of the picture data is larger than that of the PDF page, executing a cutting step:
firstly, creating a cutting box with the same size as a PDF page, and keeping the position of the cutting box in a browser consistent with the position of a canvas Dom node in the browser;
then, the picture data is used as a background picture of the cutting frame, and the attribute value of the background picture of the cutting frame is set as normal;
finally, sequentially changing the value of the background picture of the cutting frame on the Y axis in sequence to obtain the picture data in the cutting frame, and simultaneously exporting the picture data from the browser into a PDF file by calling a PDF plug-in; the change is performed in units of the height of the PDF page each time the value of the background picture of the crop box on the Y axis is changed.
Further, the height of the canvas Dom node is the height of the canvas scaling Dom node.
Further, the canvas is scaled by 3.
Further, when the browser is a chrome browser or a firefox browser, if the height of the canvas Dom node is greater than 32767px, sequentially reducing the height of the Dom node by m, and then calculating the canvas Dom node until the height of the canvas Dom node is less than 32767 px; wherein m > 0.
Further, the position of the cloned Dom node in the browser is the upper left corner of the browser.
Further, the page height calculating step includes: firstly, calculating to obtain a proportional coefficient according to the width and the height of standard A4 paper, and then calculating to obtain the height of a PDF page occupied by a clone Dom node during exporting according to the proportional coefficient and the width of the PDF page occupied by the clone Dom node during exporting; wherein, the proportionality coefficient is the width of standard A4 paper/the height of standard A4 paper.
Further, the height of each Dom child node occupying the PDF page during exporting is: the height of PDF page occupied by the cloned Dom node during the derivation is page-marginop + marginbotton; wherein, the page, the marginop and the marginbotton are respectively the self-defined attributes of the DIV label corresponding to the corresponding Dom child node.
The second purpose of the invention is realized by adopting the following technical scheme:
an apparatus for exporting a PDF by a web report includes a memory and a processor, where the memory stores a program that can be executed on the processor, and the program is a computer program, and the processor executes the computer program to implement the steps of a method for exporting a PDF by a web report according to one of the objects of the present invention.
The third purpose of the invention is realized by adopting the following technical scheme:
a storage medium being a computer readable storage medium having stored thereon a program being a computer program, which when executed by a processor is the steps of a method of web report derivation PDF, as employed in one of the objects of the present invention.
Compared with the prior art, the invention has the beneficial effects that:
the invention sets the self-defined attribute according to the business requirement, and then dynamically calculates the height of the PDF page occupied by the report when the report is exported according to the set self-defined attribute, so that the web report is correctly exported into a PDF file, and the efficiency of exporting the report is greatly improved.
Drawings
FIG. 1 is a schematic diagram of a report display in a web page provided by the present invention;
FIG. 2 is a schematic diagram of a report displayed in a PDF page according to the present invention;
FIG. 3 is a flowchart of a method for exporting a PDF from a web report according to the present invention;
fig. 4 is a flowchart of step S8 in fig. 3;
fig. 5 is a block diagram of an apparatus for exporting a PDF by a web report according to the present invention.
In the figure: 11. a memory; 12. a processor; 13. a communication bus; 14. a network interface.
Detailed Description
The present invention will be further described with reference to the accompanying drawings and the detailed description, and it should be noted that any combination of the embodiments or technical features described below can be used to form a new embodiment without conflict.
Example one
In order to solve the problem of low efficiency in exporting reports in a business system, the invention provides a method for exporting PDF (Portable Document Format) of a Web (WorldWide Web, webpage) report, which adopts a method with a pure front end and without dependence of a third-party plug-in, realizes the ready-to-use and plug-in component library, realizes the report display effect of dividing the Web interface by the A4 paper standard by appointing a custom HTML (Hypertext Markup Language) label and the height of a header and a footer, dynamically realizes the page rendering export of the interface, and realizes the function of automatically and quickly exporting the report.
In order to realize the export of the report, the invention also sets custom attributes for the HTML tags. When exporting, firstly, the custom attribute is inserted into an HTML label of an HTML document corresponding to the report according to the exporting effect, then the height and the like of the report when exporting to a PDF page are calculated dynamically, and finally a PDF plug-in is called to export the report. In addition, the width and height referred to herein are the width and height occupied in the PDF page by the rendered corresponding tag when the HTML document is exported. Typically identified by attributes and attribute values of HTML tags.
As shown in table 1, the present invention provides the custom attributes of this embodiment:
Figure BDA0002302578050000061
TABLE 1
The standard a4 paper sheet in this example has a resolution of 72 pixels/inch, and has a width and height of: 595 × 842. The dimensions of the standard a4 paper referred to herein are all as described above.
In addition, the custom attribute of the invention is not different from the attribute of the existing HTML tag during rendering, and the custom attribute can be applied to the HTML tag and takes effect during rendering for report export.
As shown in fig. 3, the present invention provides a preferred embodiment, a method for exporting a PDF by a web report, comprising the following steps:
and step S1, setting the custom attribute according to the service requirement. The custom attributes are shown in table 1 above.
Step S2: and when an export instruction is received, inserting custom attributes into corresponding tags in the HTML document corresponding to the report according to the service requirements, and assigning values to each custom attribute.
The business requirement refers to a specific effect when the report is exported, and the effect can be set according to the requirement of a client. For example, custom attributes may be inserted in the corresponding HTML tags in the report preview interface.
Due to the nature of HTML tag rendering, it is common to render one DIV tag as one block. Therefore, when the report is exported, differentiation is also performed according to the DIV tag.
For example, when a DOM node corresponding to one DIV tag needs to be rendered in standard a4 paper when being exported, an attribute name of paper needs to be inserted into the DIV tag, which indicates that a block represented by the DIV tag occupies one PDF page when being exported. In addition, the size of the PDF page in the present application is also referred to herein as the size of a standard a4 paper. The DOM node is the DOM node corresponding to the DIV tag in the original HTML document. According to the invention, the DOM node is copied, and then the rendered height is dynamically calculated and derived on the copied Dom node, so that the situation that the display format of the original HTML document is influenced by changing the tag in the original HTML document can be avoided.
For another example, when a DOM node corresponding to one DIV tag is exported and needs to be displayed by multiple PDF pages, a custom attribute page is added to the DIV tag, and the value of the custom attribute page is the number of pages of the PDF page occupied by the DIV tag during export rendering.
In addition, generally, two adjacent pages of the report are close to each other when the report is displayed on the web interface, but each page of the word document or the PDF page has headers, footers, and blanks, and the headers and the footers occupy corresponding spaces, as shown in fig. 1, the display effect of the report content in the web page is shown, as shown in fig. 2, the display effect of the report content in the word document or the PDF page is shown, and at this time, the report content needs to occupy multiple PDF pages when being exported. Therefore, in order to correctly export the DOM node corresponding to the DIV tag, it is further necessary to insert a custom attribute into the DIV tag of one page occupied during export: marginop (top margin), marginbottom (bottom margin) (distance indicated by arrow in fig. 2). In addition, the numerical values of margin and margin can be set according to business requirements. For example, when a report is placed on a page of paper, the lower margin is set to be 20, and then the custom attribute can be added in the corresponding DIV tag of the page: margin bottom ═ 20.
By adding the custom attributes into the DIV tags of the HTML document corresponding to the report, the height of the DOM node corresponding to each DIV tag during rendering and exporting can be dynamically calculated in the subsequent process according to the set custom attribute values.
In addition, the embodiment also provides a code to specifically explain that the custom attribute is inserted into the DIV tag:
Figure BDA0002302578050000081
as shown above, in this case, name ═ paper "margin ═ 0" page ═ 1"margin bottom ═ 20" is a newly added custom attribute.
Step S3, acquiring a DOM node corresponding to the DIV tag from the HTML document according to the ID value of the DIV tag in the HTML document corresponding to the report, creating a cloned Dom node according to the DOM node, assigning all HTML tag contents in the DOM node to the cloned Dom node, and setting the position of the cloned Dom node in the browser. Specifically, the DOM node corresponding to the DIV tag with the ID of the printDivId may be obtained by document.
The position of the clone Dom node is set in the upper left corner of the screen in this embodiment. Such as setting the following attributes in the DIV tag: position-99999, top-0, left-0.
Where position is the position of the cloned Dom node in the browser, and its value absolute is expressed as an absolute position. The zIndex is a value of a layer displayed by a cloned Dom node in a browser, the larger the value is, the uppermost layer is displayed, the smaller the value is, the bottommost layer is displayed, and if the layer is opaque, the layer can be hidden and invisible.
Step S4, calculating the total height of the PDF page occupied by the clone Dom node during exporting according to the size of the standard A4 paper and the width of the PDF page occupied by the clone Dom node during exporting; the size of the PDF page is that of a standard a4 sheet.
When calculating the total height of the PDF page occupied by the clone Dom node during exporting, firstly calculating a proportional coefficient according to the width and the height of the standard A4 paper, and then calculating the height of the PDF page occupied by the clone Dom node during exporting according to the width and the proportional coefficient of the PDF page occupied by the clone Dom node during exporting.
Among them, the width and height of the standard a4 paper herein are: 595. 841. Thus, the proportionality coefficient is: 595/841. In addition, the width and height of the standard A4 paper referred to herein are all the width and height of the standard A4 paper when the resolution is 72 pixels/inch.
In addition, the width of the PDF page occupied by the clone Dom node during the exporting process is obtained from the DIV label corresponding to the clone Dom node. For example, the width of the PDF page occupied by the corresponding DIV tag during rendering is determined by the attribute value offsetWidth in the DIV tag, that is, the width of the PDF page occupied by the cloned Dom node corresponding to the DIV tag during exporting.
Step S5, obtaining, according to the clone Dom node, Dom child nodes corresponding to all DIV tags having the custom attribute name ═ paper, and then respectively calculating, according to the custom attributes marginop, marginbottom, page of the DIV tag corresponding to each Dom child node and the height of the clone Dom node in the PDF page when exporting, the height of the corresponding Dom child node in the PDF page when exporting.
Because multiple DIV tags may exist in the DOM node corresponding to the report, when the DOM node corresponding to a certain DIV tag needs to occupy the PDF page during exporting, the name in the custom attribute added to the corresponding DIV tag is equal to the paper. Namely: when a certain DIV tag has a custom attribute name equal to paper, it is indicated that the DOM node corresponding to the current DIV tag needs to use A4 paper when being exported, that is, the PDF page is occupied. This may occur when a report occupies multiple pages of PDF pages at the time of export.
Therefore, in order to ensure that the DOM node corresponding to the report is correctly derived, the height of the PDF page where the DIV sub-tag derivation of the DIV sub-tag in the DIV corresponding to the DOM node corresponding to the report is located needs to be calculated.
And acquiring Dom child nodes corresponding to all DIV labels with the custom attribute name being paper according to the cloned Dom node, and then respectively calculating the height of each Dom child node occupying the PDF page when being exported.
Wherein, page represents the page number of PDF page, margin represents the upper margin of PDF page, and margin represents the lower margin of PDF page.
Specifically, the height of the PDF page occupied by the Dom child node corresponding to each DIV tag during exporting is equal to the height of the cloned Dom node occupying the PDF page during exporting.
For example, when the page is 2, the true height of the PDF page where the Dom child node is located at the time of derivation is 2-the height of the PDF page where the clone Dom node is located at the time of derivation + the bottom margin.
The height of the page of the PDF is occupied when the PDF is exported. Since each Dom child node here can prepare for each succeeding Dom child node to generate a picture during rendering.
Step S6, add clone Dom node to Body tag of HTML document of browser. Generally, a clone Dom node is added to a Body element (tag) of an HTML document of a browser according to method div. Where the Body element defines the Body of the document, which includes all the contents of the document, such as text, superview, images, tables, lists, etc.
And step S7, creating a canvas Dom node, and keeping the position of the canvas Dom node in the browser consistent with the position of the clone Dom node in the browser. Since the position of the cloned Dom node in the browser is the upper left corner of the browser, the position of the canvas Dom node in the browser is also set in the upper left corner of the browser. In addition, the width and height of the canvas Dom node are determined according to the width and height of the DOM node and the canvas scaling.
And the width of the canvas Dom node is the width of the Dom node.
Canvas Dom node height canvas scale.
Generally, the canvas scale defaults to 3. However, since the chrome browser, firefox browser, generally limits the height of the canvas to 32767 px.
When the browser is a chrome browser or a firefox browser, the height of the created canvas Dom node cannot be larger than 32767 px. I.e., height of the canvas scale DOM node <32767 px.
Thus, when the default value of canvas scale, 3, is less than or equal to 32767px of the height of the original DOM, then the canvas scale is 3 at this time.
And when the default value of the canvas scaling is 3 × the height of the original DOM is larger than 32767px, subtracting the preset value from the height of the DOM node in sequence to obtain the height of a new original DOM, and then calculating and judging the height of the 3 × new original DOM and the size of 32767px each time until the calculated result is smaller than 32767 px. Wherein the preset value is generally 0.1.
When the browser is other browsers, the canvas scaling scale is defaulted to 3 directly.
And step S8, converting the contents of all HTML tags of the clone Dom node into picture data in the canvas Dom node through an HTML2canvas function, and calling a PDF plug-in to export the picture data from the browser into a PDF file.
That is, the HTML text content is converted into picture data by calling the function HTML2canvas () and placed in the canvas. Then, when exporting the picture data from the browser to a PDF file, the picture data is exported from the browser to a PDF file by calling the function pdf.save () in the PDF plug-in.
In addition, since the generated picture data in the present application is the entire DOM node, when the DOM node needs to be displayed with multiple pages of PDF during exporting, the height of the picture data needs to be greater than the height of a PDF page, and if the PDF plug-in is directly called to generate a PDF file at this time, part of the picture data is lost, so that the height of the picture data needs to be determined during exporting the PDF file.
Namely: when the height of the picture data is less than or equal to the height of one PDF page, the picture data is directly generated through a PDF plug-in to be exported to PDF from a browser.
When the height of the picture data is greater than that of one PDF page, data loss may result during exporting, and therefore the picture data needs to be cut, cut into a plurality of pictures, and placed in a plurality of PDF pages.
As shown in fig. 4, the present invention provides a specific clipping process:
and step S81, creating a cutting box with the same size as the PDF page, and keeping the position of the cutting box in the browser consistent with the position of the canvas Dom node in the browser. For example, a crop box is represented by a DIV tag, and then the attribute value of the DIV tag is set according to the PDF page size and the position of the canvas Dom node in the browser.
Step S82 is to take the picture data as a background picture of the crop box and set the attribute value of the background picture of the crop box to norrepeat.
Step S83, sequentially changing the value of the background picture of the cutting frame on the Y axis to obtain the picture data in the cutting frame, and exporting the picture data from the browser into a PDF file by calling a PDF plug-in; the change is performed in units of the height of the PDF page each time the value of the background picture of the crop box on the Y axis is changed. That is, the position of the background picture in the crop box is modulated by changing the value of the background position attribute of the DIV tag. The positions of the background pictures in the cutting frame are sequentially changed by taking the height of the PDF page as a unit, so that the picture data in the current cutting frame obtained after each change is the content of one page of PDF page.
That is, when the content of the report cannot be displayed through one page, the generated canvas picture needs to be cropped through the cropper, and finally, a plurality of canvas pictures are saved in one pdf file.
Example two
The invention provides a device for exporting PDF from a web report. As shown in fig. 5, an internal structure diagram of the apparatus for exporting a PDF from a web report according to an embodiment of the present invention is provided.
In this embodiment, the device for deriving the PDF from the web report may be a PC (Personal Computer), or may be a terminal device such as a smart phone, a tablet Computer, or a portable Computer. The device for exporting PDF by the web report at least comprises: a processor 12, a communication bus 13, a network interface 14, and a memory 11.
The memory 11 includes at least one type of readable storage medium, which includes a flash memory, a hard disk, a multimedia card, a card type memory (e.g., SD or DX memory, etc.), a magnetic memory, a magnetic disk, an optical disk, and the like. The memory 11 may be an internal storage unit of the device of the web report export PDF, such as a hard disk of the device of the web report export PDF, in some embodiments. The memory 11 may also be an external storage device of the apparatus for deriving the web report PDF, for example, a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), and the like, which are provided on the apparatus for deriving the web report PDF. Further, the memory 11 may also include both an internal storage unit and an external storage device of the apparatus for web report exporting PDF. The memory 11 may be used not only to store application software installed in the device for a web report export PDF and various types of data, such as codes of programs of the web report export PDF, etc., but also to temporarily store data that has been output or is to be output.
The processor 12 may be a Central Processing Unit (CPU), controller, microcontroller, microprocessor or other data Processing chip in some embodiments, and is used for running program codes stored in the memory 11 or Processing data, such as a program for executing a web report export PDF, and the like.
The communication bus 13 is used to realize connection communication between these components.
The network interface 14 may optionally include a standard wired interface, a wireless interface (e.g., WI-FI interface), and is typically used to establish a communication link between the device for deriving a PDF from a web report and other electronic devices.
Optionally, the apparatus for deriving a PDF from a web report may further include a user interface, where the user interface may include a Display (Display), an input unit such as a Keyboard (Keyboard), and the optional user interface may also include a standard wired interface and a wireless interface. Alternatively, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED (Organic Light-emitting diode) touch device, or the like. The display, which may also be referred to as a display screen or display unit, is suitable for displaying information processed in the device for web report export PDF and for displaying a visual user interface.
While fig. 5 shows only the means of the web report export PDF having the components 11-14 and the program of the web report export PDF, it will be understood by those skilled in the art that the structure shown in fig. 5 does not constitute a limitation of the means of the web report export PDF, and may include fewer or more components than shown, or combine certain components, or a different arrangement of components.
In the embodiment of the apparatus for exporting a web report PDF shown in fig. 5, a program for exporting a web report PDF is stored in the memory 11; the processor 12 executes the program of the web report export PDF stored in the memory 11 to implement the following steps:
step S1: setting a custom attribute according to the service requirement; the custom attributes are shown in table 1.
Step S2: when an export instruction is received, inserting custom attributes into corresponding tags in an HTML document corresponding to a report according to business requirements, and assigning values to each custom attribute;
step S3: acquiring a DOM node corresponding to a DIV tag from an HTML document according to the ID of the DIV tag in the HTML document corresponding to the report, creating a cloned Dom node according to the DOM node, and simultaneously assigning the contents of all HTML tags in the DOM node to the cloned Dom node and setting the position of the cloned Dom node in a browser;
step S4: calculating the height of the PDF page occupied by the clone Dom node during exporting according to the size of the standard A4 paper and the width of the PDF page occupied by the clone Dom node during exporting; the size of the PDF page is the size of a standard A4 paper; the width of the PDF page occupied by the cloned Dom node during the exporting process is obtained from a DIV label corresponding to the cloned Dom node;
step S5: obtaining Dom child nodes corresponding to all DIV labels with custom attribute name being paper according to a clone Dom node, and then respectively calculating the height of PDF page occupied by the corresponding Dom child node during exporting according to the custom attributes marginop, marginbottom and page of the DIV label corresponding to each Dom child node and the height of the clone Dom node during exporting;
step S6: adding the clone Dom node to a Body tag of an HTML document of the browser;
step S7: setting and creating a canvas Dom node according to the width and the height of the DOM node and the canvas scaling ratio, and keeping the position of the canvas Dom node in the browser consistent with the position of the cloned Dom node in the browser;
step S8: and converting the contents of all HTML tags of the clone Dom node into picture data in the canvas Dom node through an HTML2canvas function, and calling a PDF plug-in to export the picture data from the browser into a PDF file.
EXAMPLE III
The invention also provides a storage medium, which is a computer-readable storage medium and on which a program for deriving a PDF from a web report is stored, wherein the program for deriving a PDF from a web report is a computer program, and when being executed by a processor, the program for deriving a PDF from a web report realizes the following steps:
step S1: setting a custom attribute according to the service requirement; the custom attributes are shown in table 1.
Step S2: when an export instruction is received, inserting custom attributes into HTML tags corresponding to the report according to business requirements, and assigning values to each custom attribute;
step S3: acquiring a DOM node corresponding to a DIV tag from an HTML document according to the ID of the DIV tag in the HTML document corresponding to the report, creating a cloned Dom node according to the DOM node, and simultaneously assigning the contents of all HTML tags in the DOM node to the cloned Dom node and setting the position of the cloned Dom node in a browser;
step S4: calculating the height of the PDF page occupied by the clone Dom node during exporting according to the size of the standard A4 paper and the width of the PDF page occupied by the clone Dom node during exporting; the size of the PDF page is the size of a standard A4 paper; the width of the PDF page occupied by the cloned Dom node during the exporting process is obtained from a DIV label corresponding to the cloned Dom node;
step S5: obtaining Dom child nodes corresponding to all DIV labels with custom attribute name being paper according to a clone Dom node, and then respectively calculating the height of PDF page occupied by the corresponding Dom child node during exporting according to the custom attributes marginop, marginbottom and page of the DIV label corresponding to each Dom child node and the height of the clone Dom node during exporting;
step S6: adding the clone Dom node to a Body tag of an HTML document of the browser;
step S7: setting and creating a canvas Dom node according to the width and the height of the DOM node and the canvas scaling ratio, and keeping the position of the canvas Dom node in the browser consistent with the position of the cloned Dom node in the browser;
step S8: and converting the contents of all HTML tags of the clone Dom node into picture data in the canvas Dom node through an HTML2canvas function, and calling a PDF plug-in to export the picture data from the browser into a PDF file.
The above embodiments are only preferred embodiments of the present invention, and the protection scope of the present invention is not limited thereby, and any insubstantial changes and substitutions made by those skilled in the art based on the present invention are within the protection scope of the present invention.

Claims (10)

1. A method for exporting PDF by a web report is characterized by comprising the following steps:
and an attribute definition step: setting a custom attribute according to the service requirement, wherein the custom attribute is shown in the following table;
Figure FDA0002302578040000011
and an attribute inserting step: when an export instruction is received, inserting custom attributes into corresponding tags in an HTML document corresponding to a report according to business requirements, and assigning values to each custom attribute;
cloning step: acquiring a DOM node corresponding to a DIV tag from an HTML document according to the ID value of the DIV tag in the HTML document corresponding to the report, creating a cloned Dom node according to the DOM node, assigning the contents of all HTML tags in the DOM node to the cloned Dom node and setting the position of the cloned Dom node in a browser;
a first calculation step: calculating the height of the PDF page occupied by the clone Dom node during exporting according to the size of the standard A4 paper and the width of the PDF page occupied by the clone Dom node during exporting; the size of the PDF page is the size of a standard A4 paper; the width of the PDF page occupied by the cloned Dom node during the exporting process is obtained from a DIV label corresponding to the cloned Dom node;
a second calculation step: obtaining Dom child nodes corresponding to all DIV labels with custom attribute name being paper according to a clone Dom node, and then respectively calculating the height of PDF page occupied by the corresponding Dom child node during exporting according to the custom attributes marginop, marginbottom and page of the DIV label corresponding to each Dom child node and the height of the clone Dom node during exporting;
an adding step: adding the clone Dom node to a Body tag of an HTML document of the browser;
canvas creation step: setting and creating a canvas Dom node according to the width and the height of the DOM node and the canvas scaling ratio, and keeping the position of the canvas Dom node in the browser consistent with the position of the cloned Dom node in the browser;
and (3) a derivation step: and converting the contents of all HTML tags of the clone Dom node into picture data in the canvas Dom node through an HTML2canvas function, and calling a PDF plug-in to export the picture data from the browser into a PDF file.
2. The method for exporting PDF from a web report according to claim 1, wherein the exporting step further comprises:
when the height of the picture data is smaller than or equal to the height of the PDF page, exporting the picture data from a browser into a PDF file by calling a PDF plug-in;
when the height of the picture data is larger than that of the PDF page, executing a cutting step:
firstly, creating a cutting box with the same size as a PDF page, and keeping the position of the cutting box in a browser consistent with the position of a canvas Dom node in the browser;
then, the picture data is used as a background picture of the cutting frame, and the attribute value of the background picture of the cutting frame is set as normal;
finally, sequentially changing the value of the background picture of the cutting frame on the Y axis in sequence to obtain the picture data in the cutting frame, and simultaneously exporting the picture data from the browser into a PDF file by calling a PDF plug-in; the change is performed in units of the height of the PDF page each time the value of the background picture of the crop box on the Y axis is changed.
3. The method of claim 1, wherein the height of the canvas Dom node is the height of the canvas scale Dom node; the width of the canvas Dom node is the width of the Dom node.
4. The method of claim 3, wherein the canvas scaling is 3.
5. The method for exporting PDF from a web report according to claim 3, wherein when the browser is a chrome browser or a firefox browser, if the height of the canvas Dom node is greater than 32767px, the height of the DOM node is sequentially reduced by m and then the canvas Dom node is calculated until the height of the canvas Dom node is less than 32767 px; wherein 0.1< m < 0.3.
6. The method for exporting PDF from a web report according to claim 1, wherein the cloned Dom node is located in the upper left corner of the browser and is made invisible at the bottom of the layer.
7. The method for exporting PDF from a web report according to claim 1, wherein the step of calculating the page height comprises: firstly, calculating to obtain a proportional coefficient according to the width and the height of standard A4 paper, and then calculating to obtain the height of a PDF page occupied by a clone Dom node during exporting according to the proportional coefficient and the width of the PDF page occupied by the clone Dom node during exporting; wherein, the proportionality coefficient is the width of standard A4 paper/the height of standard A4 paper.
8. The method for exporting a PDF from a web report according to claim 1, wherein the height of each Dom child node in the PDF page during exporting is as follows: the height of PDF page occupied by the cloned Dom node during the derivation is page-marginop + marginbotton; wherein, the page, the marginop and the marginbotton are respectively the self-defined attributes of the DIV label corresponding to the corresponding Dom child node.
9. An apparatus for deriving a PDF from a web report, comprising a memory and a processor, the memory having stored thereon a program executable on the processor, the program being a computer program, characterized in that: the processor, when executing the computer program, performs the steps of a method of deriving a PDF from a web report according to any one of claims 1 to 8.
10. A storage medium which is a computer-readable storage medium having a program stored thereon, the program being a computer program, characterized in that: the computer program, when being executed by a processor, comprises the steps of a method for deriving a PDF from a web report according to any one of claims 1 to 8.
CN201911227222.6A 2019-12-04 2019-12-04 Method and device for exporting PDF (Portable document Format) from web report and storage medium Active CN111309671B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911227222.6A CN111309671B (en) 2019-12-04 2019-12-04 Method and device for exporting PDF (Portable document Format) from web report and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911227222.6A CN111309671B (en) 2019-12-04 2019-12-04 Method and device for exporting PDF (Portable document Format) from web report and storage medium

Publications (2)

Publication Number Publication Date
CN111309671A true CN111309671A (en) 2020-06-19
CN111309671B CN111309671B (en) 2023-01-31

Family

ID=71156257

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911227222.6A Active CN111309671B (en) 2019-12-04 2019-12-04 Method and device for exporting PDF (Portable document Format) from web report and storage medium

Country Status (1)

Country Link
CN (1) CN111309671B (en)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111859865A (en) * 2020-06-30 2020-10-30 深圳市中农易讯信息技术有限公司 Method, device, terminal and medium for converting PDF document
CN112036123A (en) * 2020-08-31 2020-12-04 北京奇虎鸿腾科技有限公司 PDF (Portable document Format) generation method, device and equipment based on webpage and storage medium
CN112558894A (en) * 2020-12-23 2021-03-26 广州鼎甲计算机科技有限公司 Canvas-based dynamic webpage printing method, system and storage medium
CN116383546A (en) * 2023-06-06 2023-07-04 深圳思谋信息科技有限公司 File processing method, system, computer device and computer readable storage medium

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104571977A (en) * 2014-12-05 2015-04-29 北京赛德高科铁道电气科技有限责任公司 Report printing method and system based on HTML (Hyper Text Markup Language) template
CN104714766A (en) * 2015-03-12 2015-06-17 福建工程学院 Method for direct printing in WEB report browser
CN107544952A (en) * 2017-08-28 2018-01-05 金蝶软件(中国)有限公司 Report form generation method, device, storage medium and computer equipment
CN107766359A (en) * 2016-08-17 2018-03-06 广州市动景计算机科技有限公司 A kind of method, apparatus and computing device that content of pages is converted to picture
CN107885848A (en) * 2017-11-10 2018-04-06 杭州美创科技有限公司 Web page screen-cutting method based on web technology
CN109491743A (en) * 2018-11-05 2019-03-19 上海鋆创信息技术有限公司 Visual Report Forms generation method, device, electronic equipment and storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104571977A (en) * 2014-12-05 2015-04-29 北京赛德高科铁道电气科技有限责任公司 Report printing method and system based on HTML (Hyper Text Markup Language) template
CN104714766A (en) * 2015-03-12 2015-06-17 福建工程学院 Method for direct printing in WEB report browser
CN107766359A (en) * 2016-08-17 2018-03-06 广州市动景计算机科技有限公司 A kind of method, apparatus and computing device that content of pages is converted to picture
CN107544952A (en) * 2017-08-28 2018-01-05 金蝶软件(中国)有限公司 Report form generation method, device, storage medium and computer equipment
CN107885848A (en) * 2017-11-10 2018-04-06 杭州美创科技有限公司 Web page screen-cutting method based on web technology
CN109491743A (en) * 2018-11-05 2019-03-19 上海鋆创信息技术有限公司 Visual Report Forms generation method, device, electronic equipment and storage medium

Cited By (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111859865A (en) * 2020-06-30 2020-10-30 深圳市中农易讯信息技术有限公司 Method, device, terminal and medium for converting PDF document
CN112036123A (en) * 2020-08-31 2020-12-04 北京奇虎鸿腾科技有限公司 PDF (Portable document Format) generation method, device and equipment based on webpage and storage medium
CN112036123B (en) * 2020-08-31 2024-05-10 三六零数字安全科技集团有限公司 PDF generation method, device, equipment and storage medium based on webpage
CN112558894A (en) * 2020-12-23 2021-03-26 广州鼎甲计算机科技有限公司 Canvas-based dynamic webpage printing method, system and storage medium
CN112558894B (en) * 2020-12-23 2021-11-12 广州鼎甲计算机科技有限公司 Canvas-based dynamic webpage printing method, system and storage medium
CN116383546A (en) * 2023-06-06 2023-07-04 深圳思谋信息科技有限公司 File processing method, system, computer device and computer readable storage medium
CN116383546B (en) * 2023-06-06 2023-08-11 深圳思谋信息科技有限公司 File processing method, system, computer device and computer readable storage medium

Also Published As

Publication number Publication date
CN111309671B (en) 2023-01-31

Similar Documents

Publication Publication Date Title
CN111309671B (en) Method and device for exporting PDF (Portable document Format) from web report and storage medium
US9471550B2 (en) Method and apparatus for document conversion with font metrics adjustment for format compatibility
WO2021068331A1 (en) Web page adaptive layout method, server and computer-readable storage medium
CN101128826B (en) Presentation method of large objects on small displays
US9043698B2 (en) Method for users to create and edit web page layouts
US10902193B2 (en) Automated generation of web forms using fillable electronic documents
CN110879937A (en) Method and device for generating webpage from document, computer equipment and storage medium
WO2015180422A1 (en) Page layout method and device
WO2015078159A1 (en) Webpage display method and device
CN102662616A (en) Screen graphics self-adaption method and system used for mobile terminal
EA012487B1 (en) A method of displaying data in a table
WO2016130236A1 (en) Responsive course design system and method
CN115757272A (en) Method and system for converting HTML file into OFD file
US9886426B1 (en) Methods and apparatus for generating an efficient SVG file
CN110310226B (en) Picture mosaic display method and system
CN106776527B (en) Electronic book data display method and device and terminal equipment
CN112486488A (en) Page self-adaption method applied to government affair service terminal
CN111783007A (en) Display rendering method and device, electronic equipment and storage medium
US20110055258A1 (en) Method and apparatus for the page-by-page provision of an electronic document as a computer graphic
Krause et al. CSS: Cascading Style Sheets
CN112434487B (en) Image-text typesetting method and device and electronic equipment
JP2015011420A (en) Article image reconstruction apparatus
US20160170941A1 (en) Replicating the appearance of typographical attributes and text adornments in digital publications
CN111914226A (en) Background picture generation method and device
CN113240776B (en) Method, device and storage medium for changing picture color based on canvas

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant