Method, system and equipment for generating pdf based on headless browser

Through headless browser loading and CSS engine rendering technology, it solves the layout confusion and content misalignment problems caused by absolute positioning in traditional PDF report generation, and achieves correct document flow and format.

CN118551733APending Publication Date: 2024-08-27SHENZHEN RENDERBUS TECH
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410601050.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-15
Publication Date
2024-08-27

AI Technical Summary

Technical Problem

The traditional PDF report generation method cannot form a correct document flow due to the absolute positioning of the filled information, resulting in confusing layout and misaligned content.

Method used

Use a headless browser to load the preset HTML template, render the data page through the CSS engine, form a structured tree-like data structure DOM tree, and apply style rules to generate the final calculation style and solve layout problems.

Benefits of technology

The correct document flow of data pages is realized, layout confusion and content misalignment are avoided, and the generated PDF file format is clear and reasonable.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118551733A
    Figure CN118551733A
Patent Text Reader

Abstract

The invention relates to the technical field of data processing, in particular to a method, a system and equipment for generating a pdf based on a headless browser. The method comprises the steps of obtaining to-be-filled data and a preset HTML template; loading the HTML template by utilizing a preset headless browser; based on a result of loading the HTML template by the preset headless browser, filling the data to be filled into a corresponding position in the HTML template to obtain a first data page; and finally, rendering the first data page by utilizing a preset CSS engine to obtain a pdf file in a preset form. It can be understood that according to the technical scheme provided by the invention, through the preset CSS engine, the first data page is rendered, the data page is helped to form the correct document stream, and the problems of layout disorder and content dislocation possibly caused by the fact that the correct document stream cannot be formed in a traditional pdf report generation method are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of technical data processing, and in particular to a method, system and device for generating PDF based on a headless browser. Background Art

[0002] Traditional PDF report generation methods usually adopt the method of filling data into pre-designed template images. These template images contain the basic layout, format and style of the report, but because the data filling is absolutely positioned, it cannot form a correct document flow, which may lead to confusing layout and misplaced content.

[0003] That is, in the prior art, the traditional PDF report generation method cannot form a correct document flow due to the absolute positioning of the filling information, which may lead to chaotic layout and misplaced content. Summary of the invention

[0004] In view of this, the purpose of the present invention is to provide a method, system and device for generating PDF based on a headless browser, so as to solve the problem that the correct document flow cannot be formed due to the absolute positioning of the filling information in the traditional PDF report generation method in the prior art, which may cause layout confusion and content misalignment.

[0005] According to a first aspect of an embodiment of the present invention, a method for generating a PDF based on a headless browser is provided, comprising:

[0006] Get the data to be filled and the preset HTML template;

[0007] Using a preset headless browser to load the HTML template;

[0008] Based on the result of the preset headless browser loading the HTML template, fill the data to be filled into the corresponding position in the HTML template to obtain a first data page;

[0009] The first data page is rendered using a preset CSS engine to obtain a PDF file in a preset format.

[0010] Further, the using a preset headless browser to load the HTML template includes:

[0011] Create a new tab using the default headless browser;

[0012] Use the newly created tab to load the HTML template.

[0013] Furthermore, the result of loading the HTML template based on the preset headless browser, filling the data to be filled into the corresponding position in the HTML template, and obtaining the first data page, includes:

[0014] Based on the result of the preset headless browser loading the HTML template, the document.getElementById() function is called to fill the data to be filled into the corresponding position in the HTML template.

[0015] Furthermore, after the result of loading the HTML template based on the preset headless browser, the data to be filled is filled into the corresponding position in the HTML template, and the first data page is obtained, the first data page is rendered using a preset CSS engine, and before the PDF file in a preset format is obtained, it also includes:

[0016] The first data page is parsed using the main thread in the rendering process in the preset headless browser, and a structured tree data structure DOM tree is constructed.

[0017] Furthermore, the using of a preset CSS engine to render the first data page to obtain a PDF file in a preset format includes:

[0018] Use the preset CSS engine to collect, divide and index the style rules in the style sheet;

[0019] Using the style rules, selector matching is performed for each element in the structured tree data structure DOM tree, and style setting is performed for the matched nodes;

[0020] The CSS cascading style rules are used to generate the final calculated style for the node to obtain a PDF file in a preset format.

[0021] Furthermore, the method further comprises:

[0022] Call the window.getComputedStyle() function to obtain the relevant data of the final calculated style generated for the node using CSS cascading style rules.

[0023] According to a second aspect of an embodiment of the present invention, a system for generating PDF based on a headless browser is provided, which is applied to any of the above methods for generating PDF based on a headless browser, and the system comprises:

[0024] The acquisition module is used to obtain the data to be filled and the preset HTML template;

[0025] A first data processing module, used for loading the HTML template using a preset headless browser;

[0026] A second data processing module is used to fill the data to be filled into a corresponding position in the HTML template based on the result of the preset headless browser loading the HTML template, so as to obtain a first data page;

[0027] The third data processing module is used to render the first data page using a preset CSS engine to obtain a PDF file in a preset format.

[0028] According to a third aspect of an embodiment of the present invention, a device for generating a PDF based on a headless browser is provided, the device comprising:

[0029] A memory having an executable program stored therein;

[0030] A processor is used to execute the executable program in the memory to implement the steps of any one of the above methods.

[0031] The technical solution provided by the embodiments of the present invention may have the following beneficial effects:

[0032] It is understandable that the technical solution provided by the present invention obtains the data to be filled and the preset HTML template; uses the preset headless browser to load the HTML template; based on the result of the preset headless browser loading the HTML template, fills the data to be filled into the corresponding position in the HTML template to obtain the first data page; finally, uses the preset CSS engine to render the first data page to obtain a PDF file in a preset format. It is understandable that the technical solution provided by the present invention renders the first data page through the preset CSS engine, helps the data page form a correct document flow, and solves the problem that the traditional PDF report generation method cannot form a correct document flow, which may cause layout confusion and content misalignment.

[0033] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.

[0035] Figure 1 It is a schematic diagram of a method for generating PDF based on a headless browser according to an exemplary embodiment;

[0036] Figure 2 It is a flowchart of an implementation method for generating PDF based on a headless browser according to an exemplary embodiment;

[0037] Figure 3 It is a schematic diagram of a system composition for generating PDF based on a headless browser according to an exemplary embodiment;

[0038] Figure 4 It is a schematic block diagram of a device for generating PDF based on a headless browser according to an exemplary embodiment. DETAILED DESCRIPTION

[0039] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Instead, they are merely examples of devices and methods consistent with some aspects of the present invention as detailed in the appended claims.

[0040] Embodiment 1

[0041] See also Figure 1 , Figure 1 The present invention is a schematic diagram of steps of a method for generating a PDF based on a headless browser according to an exemplary embodiment, wherein the method comprises:

[0042] S1. Get the data to be filled and the preset HTML template;

[0043] S2. Load the HTML template using a preset headless browser;

[0044] S3. Based on the result of loading the HTML template by the preset headless browser, fill the data to be filled into the corresponding position in the HTML template to obtain a first data page;

[0045] S4. Use a preset CSS engine to render the first data page to obtain a PDF file in a preset format.

[0046] In one embodiment, the data to be filled and the preset HTML template are obtained as described in step S1, including:

[0047] The data to be filled is based on the data of the business line obtained from the actual business;

[0048] The preset HTML template is a template constructed based on HTML (HyperText Markup Language), and the template content includes the basic structure and style of the report to be generated.

[0049] In one embodiment, it should be noted that before using the preset headless browser to load the HTML template, it is necessary to do the following preparations, including:

[0050] i) Template design: Design a basic HTML template, including the basic structure and style of the report

[0051] ii) Data preparation: Prepare the data to be populated in the report and ensure the accuracy and completeness of the data.

[0052] iii) Install a headless browser: Make sure you have a headless browser installed in your system, such as HeadlessChrome, to render the template content and generate PDF.

[0053] In one embodiment, as described in step S2, using a preset headless browser to load the HTML template includes:

[0054] Create a new tab using the default headless browser;

[0055] Use the newly created tab to load the HTML template.

[0056] It should be noted that the browser can create a new page for rendering by creating a tab page, so it will not cause task blocking.

[0057] The specific implementation steps are as follows: open the headless browser in the memory, then create a new tab page, and use the new tab page to load the HTML template located at the set location.

[0058] const puppeteer=require('puppeteer');

[0059] (async()=>{

[0060] const browser = await puppeteer.launch(); / / Open a headless browser in memory

[0061] const page = await browser.newPage(); / / Create a new tab page

[0062] await page.goto('file: / / / path / to / your / template.html') / / Read a template html for rendering.

[0063] In one embodiment, as described in step S3, based on the result of the preset headless browser loading the HTML template, the data to be filled is filled into the corresponding position in the HTML template to obtain the first data page, including:

[0064] Based on the result of the preset headless browser loading the HTML template, the document.getElementById() function is called to fill the data to be filled into the corresponding position in the HTML template.

[0065] In the specific implementation, / / assuming that the data object is reportData

[0066]

[0067] In one embodiment, as described in step S4, using a preset CSS engine to render the first data page to obtain a pdf file in a preset format includes:

[0068] Furthermore, the using of a preset CSS engine to render the first data page to obtain a PDF file in a preset format includes:

[0069] Use the preset CSS engine to collect, divide and index the style rules in the style sheet;

[0070] Using the style rules, selector matching is performed for each element in the structured tree data structure DOM tree, and style setting is performed for the matched nodes;

[0071] The CSS cascading style rules are used to generate the final calculated style for the node to obtain a PDF file in a preset format.

[0072] Furthermore, the method further comprises:

[0073] Call the window.getComputedStyle() function to obtain the relevant data of the final calculated style generated for the node using CSS cascading style rules.

[0074] Furthermore, after the result of loading the HTML template based on the preset headless browser, the data to be filled is filled into the corresponding position in the HTML template, and the first data page is obtained, the first data page is rendered using a preset CSS engine, and before the PDF file in a preset format is obtained, it also includes:

[0075] The first data page is parsed using the main thread in the rendering process in the preset headless browser, and a structured tree data structure DOM tree is constructed.

[0076] It should be noted that the technical solution provided by the present invention obtains the data to be filled and the preset HTML template; uses the preset headless browser to load the HTML template; based on the result of the preset headless browser loading the HTML template, fills the data to be filled into the corresponding position in the HTML template to obtain the first data page; finally, uses the preset CSS engine to render the first data page to obtain a PDF file in a preset format. It can be understood that the technical solution provided by the present invention renders the first data page through the preset CSS engine, helps the data page form a correct document flow, and solves the problem that the traditional PDF report generation method cannot form a correct document flow, which may cause layout confusion and content misalignment.

[0077] See also Figure 2 Furthermore, the technical solution provided by the present invention has the following specific implementation process:

[0078] 1. Load the template: Use a headless browser to load a pre-designed HTML template.

[0079] 2. Fill in data: Fill the prepared data into the corresponding position in the template.

[0080] 3. Render the page: Make sure that all content in the template is rendered correctly and the layout is as expected.

[0081] 4. Generate PDF: Convert the rendered page into a report in PDF format.

[0082] In the specific implementation, the detailed process and its code implementation

[0083] The following is the detailed process and code implementation of each step

[0084] Loading Template

[0085]

[0086]

[0087]

[0088] See also Figure 3 , Figure 3 The present invention is a schematic diagram of a system composition for generating PDF based on a headless browser according to an exemplary embodiment, which is applied to the method for generating PDF based on a headless browser according to any one of claims 1 to 6, and the system comprises:

[0089] The acquisition module 31 is used to obtain the data to be filled and the preset HTML template;

[0090] A first data processing module 32, configured to load the HTML template using a preset headless browser;

[0091] A second data processing module 33 is used to fill the data to be filled into a corresponding position in the HTML template based on the result of the preset headless browser loading the HTML template, so as to obtain a first data page;

[0092] The third data processing module 34 is used to render the first data page using a preset CSS engine to obtain a PDF file in a preset format.

[0093] According to a third aspect of an embodiment of the present invention, a device for generating a PDF based on a headless browser is provided, the device comprising:

[0094] See also Figure 4 , Figure 4 The present invention is a schematic block diagram of a device for generating PDF based on a headless browser according to an exemplary embodiment, wherein the device comprises:

[0095] A memory 41 on which an executable program is stored;

[0096] The processor 42 is used to execute the executable program in the memory 41 to implement the steps of any one of the above methods.

[0097] It can be understood that the same or similar parts of the above embodiments can be referenced to each other, and the contents not described in detail in some embodiments can refer to the same or similar contents in other embodiments.

[0098] It should be noted that, in the description of the present invention, the terms "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or implying relative importance. In addition, in the description of the present invention, unless otherwise specified, the meaning of "plurality" refers to at least two.

[0099] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, segment or portion of code that includes one or more executable instructions for implementing the steps of a specific logical function or process, and the scope of the preferred embodiments of the present invention includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by those skilled in the art to which the embodiments of the present invention belong.

[0100] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0101] A person skilled in the art may understand that all or part of the steps in the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.

[0102] In addition, each functional unit in each embodiment of the present invention may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.

[0103] The storage medium mentioned above can be a read-only memory, a magnetic disk or an optical disk, etc.

[0104] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.

[0105] Although the embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are exemplary and are not to be construed as limitations of the present invention. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present invention.

Claims

1. A method for generating PDF based on a headless browser, characterized in that: The method comprises: Get the data to be filled and the preset HTML template; Using a preset headless browser to load the HTML template; Based on the result of the preset headless browser loading the HTML template, fill the data to be filled into the corresponding position in the HTML template to obtain a first data page; The first data page is rendered using a preset CSS engine to obtain a PDF file in a preset format.

2. The method according to claim 1, characterized in that The step of loading the HTML template by using a preset headless browser includes: Create a new tab using the default headless browser; Use the newly created tab to load the HTML template.

3. The method according to claim 1, characterized in that The method of filling the data to be filled into a corresponding position in the HTML template based on the result of loading the HTML template by the preset headless browser to obtain a first data page includes: Based on the result of the preset headless browser loading the HTML template, the document.getElementById() function is called to fill the data to be filled into the corresponding position in the HTML template.

4. The method according to claim 1, characterized in that The method further includes: after the result of loading the HTML template based on the preset headless browser, filling the data to be filled into the corresponding position in the HTML template to obtain the first data page, and rendering the first data page using a preset CSS engine to obtain a PDF file in a preset format: The first data page is parsed using the main thread in the rendering process in the preset headless browser, and a structured tree data structure DOM tree is constructed.

5. The method according to claim 1, characterized in that The using of a preset CSS engine to render the first data page to obtain a PDF file in a preset format includes: Use the preset CSS engine to collect, divide and index the style rules in the style sheet; Using the style rules, selector matching is performed for each element in the structured tree data structure DOM tree, and style setting is performed for the matched nodes; The CSS cascading style rules are used to generate the final calculated style for the node to obtain a PDF file in a preset format.

6. The method according to claim 5, characterized in that The method further comprises: Call the window.getComputedStyle() function to obtain the relevant data of the final calculated style generated for the node using CSS cascading style rules.

7. A system for generating PDF based on a headless browser, applied to the method for generating PDF based on a headless browser as described in any one of claims 1 to 6, characterized in that: The system comprises: The acquisition module is used to obtain the data to be filled and the preset HTML template; A first data processing module, used for loading the HTML template using a preset headless browser; A second data processing module is used to fill the data to be filled into a corresponding position in the HTML template based on the result of the preset headless browser loading the HTML template, so as to obtain a first data page; The third data processing module is used to render the first data page using a preset CSS engine to obtain a PDF file in a preset format.

8. A device for generating PDF based on a headless browser, characterized in that: The device comprises: a memory having an executable program stored therein; A processor, configured to execute the executable program in the memory to implement the steps of the method according to any one of claims 1 to 6.

Citation Information

Cited By

  • Report generation method and system supporting custom paging and process monitoring

    CN121958684A