Front-end dom element-to-pdf downloading method, system and equipment

By separating the dom elements and converting them into pdf using the html2canvas and jspdf library, the problem of unclear and incomplete content when converting the front-end dom elements to pdf is solved, efficient and clear PDF download is achieved, and configuration dependency is reduced, and all browsing is supported. utensil.

CN118981577APending Publication Date: 2024-11-19CHINA ELECTRONICS CLOUD DIGITAL INTELLIGENCE TECH CO LTD

Patent Information

Application Number
CN202411012811.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-26
Publication Date
2024-11-19

AI Technical Summary

Technical Problem

The prior art has problems such as unclear content, incompleteness, high configuration dependence and poor compatibility when converting front-end dom elements to PDF. Especially when downloading local elements and paging downloads, truncation or incomplete display is prone to occur.

Method used

Create a list of child elements by separating dom elements, convert it into canvas picture list data, use html2canvas library to generate pictures, calculate the position of the picture on pdf based on the aspect ratio of the picture and the size parameters of the pdf instance, and draw the picture using jspdf library On PDF, ensure the integrity and clarity of the content.

Benefits of technology

It realizes efficient and clear download of local or full page PDF content, avoids truncation problems, reduces configuration dependencies, supports all browsers, and improves application flexibility and compatibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118981577A_ABST
    Figure CN118981577A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of front-end element conversion, and provides a front-end dom element-to-pdf downloading method, system and equipment, and the method comprises the steps: constructing a dom sub-element list through separating the dom elements, and converting the sub-elements in the dom sub-element list into canvas picture list data; creating a pdf instance according to the canvas picture list data, and obtaining the size position parameter of the picture on the pdf according to the size parameter of the created pdf instance and the aspect ratio of the picture; and drawing the picture on the pdf according to the size and position parameters of the picture on the pdf, and downloading the pdf file drawn with the picture. According to the method, the system and the equipment for downloading the front-end dom element to the pdf, local or whole-page pdf content can be efficiently and clearly downloaded, the paging cannot have a truncation problem, does not depend on other configurations, can be flexibly inserted and used at any time, has no compatibility problem, and supports all browsers.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of front-end element conversion, and in particular to a method, system and device for converting front-end DOM elements into PDF downloads. Background Art

[0002] At present, there are two mainstream solutions for downloading and exporting front-end DOM elements as PDF, one is back-end processing and the other is front-end processing.

[0003] The backend processing solution is to simulate opening a browser in a virtual machine, and after the content is loaded, the page is cut into images and converted into PDF for download. In actual applications, the backend processing solution has the following shortcomings: 1. There are requirements for the simulated browser version, which requires the simulated browser version to support the front-end JavaScript syntax used by the page, such as ES6-ES11 and other syntaxes. When the browser version does not support the front-end syntax used by the page, it will lead to the inability to parse the JavaScript script, which will cause the page to be opened in the browser with an error message and display blank, and the correct content cannot be intercepted and downloaded; 2. It does not support the download of partial elements, and can only download the entire page.

[0004] There are two types of front-end processing solutions. The first type is to use domtoimage to convert DOM elements into images, and then use jspdf to convert images into PDFs for downloading. In actual applications, when the page data is less than one page of PDF, it will cause incomplete horizontal screenshots of the content. The second type is to use html2canvas to convert DOM elements into images, and then use jspdf to convert images into PDFs for downloading. In actual applications, this solution has the following shortcomings: 1. If it is a single-page download, there is a limit on the size of DOM elements. When the DOM elements exceed the limit value, there is an incomplete display problem; 2. If it is a paged download, there is a problem of table text truncation.

[0005] Therefore, how to provide a method for converting front-end DOM elements to PDF, improve the clarity and completeness of PDF content, reduce configuration dependence, and improve application flexibility and compatibility has become a technical problem that needs to be solved urgently. Summary of the invention

[0006] In view of this, in order to overcome the deficiencies of the prior art, the present invention aims to provide a method, system and device for converting front-end DOM elements to PDF downloads.

[0007] According to a first aspect of the present invention, a method for converting a front-end DOM element to a PDF download is provided, the method comprising:

[0008] Build a DOM sub-element list by separating DOM elements, and convert the sub-elements in the DOM sub-element list into canvas image list data;

[0009] Create a PDF instance based on the canvas image list data, and obtain the size and position parameters of the image on the PDF based on the size parameters of the created PDF instance and the aspect ratio of the image;

[0010] Draw the image on PDF according to the size and position parameters of the image on PDF, and download the PDF file with the image drawn on it.

[0011] Preferably, in the method for converting front-end DOM elements to PDF download of the present invention, a DOM sub-element list is constructed by separating DOM elements, including:

[0012] Separate the DOM elements to be exported into several blocks of sub-elements, and set the same style name for each block of sub-elements;

[0013] Get the DOM sub-element according to the set style name, and build a DOM sub-element list based on the obtained DOM sub-element.

[0014] Preferably, in the method of converting front-end DOM elements to PDF download of the present invention, DOM sub-elements are obtained according to the set style name, and a DOM sub-element list is constructed according to the obtained DOM sub-elements, including: using the document.querySelectorAll method to obtain DOM sub-elements, and constructing a DOM sub-element list according to the obtained DOM sub-elements.

[0015] Preferably, in the method for converting front-end DOM elements to PDF download of the present invention, a DOM sub-element list is constructed by separating DOM elements, and further comprises: separating DOM elements according to the relevance or data size of DOM elements on the page.

[0016] Preferably, in the method for converting front-end DOM elements to PDF download of the present invention, sub-elements in the DOM sub-element list are converted into canvas image list data, including: looping through the constructed DOM sub-element list, and using html2canvas to convert each sub-element into canvas image list data.

[0017] Preferably, in the method of converting front-end DOM elements to PDF download of the present invention, a PDF instance is created according to canvas image list data, including: calculating and obtaining the maximum image height value and the maximum image width value according to the canvas image list data, and using a jspdf plug-in to create a PDF instance according to the obtained maximum image height value and the maximum image width value.

[0018] Preferably, in the method for converting a front-end DOM element to a PDF download of the present invention, the size and position parameters of the image on the PDF are obtained according to the size parameters of the created PDF instance and the aspect ratio of the image, including:

[0019] Get the width of the created PDF instance by calling the internal getWidth method of the created PDF instance;

[0020] Set the default horizontal margin gap value of PDF, and use the difference between the width of PDF instance and the default horizontal margin gap value of PDF as the width of the image on PDF;

[0021] The height of each image on PDF is calculated based on the width of the image on PDF and the aspect ratio of the image itself.

[0022] Preferably, in the method of converting the front-end DOM element to PDF download of the present invention, the picture is drawn on the PDF according to the size and position parameters of the picture on the PDF, and the PDF file with the picture drawn is downloaded, including: using the addImage method of jspdf, drawing the picture on the PDF according to the width of each picture on the PDF, the height on the PDF and the default gap value of the horizontal blank space of the PDF, and using the jspdf download save method to download the PDF file.

[0023] According to a second aspect of the present invention, a system for converting front-end DOM elements to PDF downloads is provided, the system comprising a conversion download server, the conversion download server being used to: construct a DOM sub-element list by separating DOM elements, and convert the sub-elements in the DOM sub-element list into canvas image list data; create a PDF instance according to the canvas image list data, and obtain the size and position parameters of the image on the PDF according to the size parameters of the created PDF instance and the aspect ratio of the image; draw the image on the PDF according to the size and position parameters of the image on the PDF, and download the PDF file with the image drawn on it.

[0024] According to a third aspect of the present invention, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in the first aspect of the present invention when executing the program.

[0025] The method, system and device for converting front-end DOM elements to PDF download of the present invention combine the html2canvas library and the jspdf library to separate page elements, set a fixed style name for each part, obtain the DOM sub-element list that needs to be exported from the page according to the style name, loop through the DOM sub-element list to convert each part of the DOM element into canvas image list data using the html2canvas library, then loop through the canvas image list data to draw each image on each page of PDF using the jspdf library, and can efficiently and clearly download partial or full-page PDF content, without truncation problems in paging, independent of other configurations, flexible plug-and-play, no compatibility issues, and support all browsers. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0027] Figure 1 This is an example diagram of a system for converting front-end DOM elements to PDF downloads according to an embodiment of the present invention;

[0028] Figure 2 This is an example diagram of the steps of a method for converting a front-end DOM element to a PDF download according to an embodiment of the present invention;

[0029] Figure 3 A schematic diagram of the structure of the device provided by the present invention. DETAILED DESCRIPTION

[0030] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.

[0031] It should be noted that the following embodiments and features in the embodiments may be combined with each other in the absence of conflict; and, based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making any creative work are within the scope of protection of the present disclosure.

[0032] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.

[0033] The technical principles of the present invention are as follows:

[0034] By separating DOM elements, the elements to be downloaded are divided into blocks. The corresponding images are generated through html2canvas, the highest image height is calculated to set the height of each PDF page, and the position of the image on the PDF is dynamically calculated to draw the image on each PDF page, and then the PDF file is downloaded through the methods in the jspdf library.

[0035] Figure 1 This is an example diagram of a system for converting front-end DOM elements to PDF downloads according to an embodiment of the present invention. Figure 1 As shown, the system may include a conversion download server 101, a communication network 102 and / or one or more conversion download clients 103. Figure 1 The example in FIG. 1 is a plurality of conversion download clients 103 .

[0036] The conversion download server 101 can be any suitable server for storing information, data, programs and / or any other suitable type of content. In some embodiments, the conversion download server 101 can perform appropriate functions. For example, in some embodiments, the conversion download server 101 can be used to convert front-end DOM elements to PDF. As an optional example, in some embodiments, the conversion download server 101 can be used to realize the conversion of front-end DOM elements to PDF by constructing a DOM sub-element list by separating DOM elements. For example, the conversion download server 101 can be used to: construct a DOM sub-element list by separating DOM elements, convert the sub-elements in the DOM sub-element list into canvas picture list data; create a PDF instance according to the canvas picture list data, obtain the size and position parameters of the picture on the PDF according to the size parameters of the created PDF instance and the aspect ratio of the picture; draw the picture on the PDF according to the size and position parameters of the picture on the PDF, and download the PDF file with the picture drawn.

[0037] As another example, in some embodiments, the conversion download server 101 can send the method of converting the front-end DOM element to PDF download to the conversion download client 103 for the user to use according to the request of the conversion download client 103.

[0038] As an optional example, in some embodiments, the conversion download client 103 is used to provide a visual conversion download interface, which is used to receive a user's selection input operation to convert the front-end DOM element into PDF and download it, and, in response to the selection input operation, obtain the conversion download interface corresponding to the option selected by the selection input operation from the conversion download server 101 and display the conversion download interface, wherein the conversion download interface at least displays information on converting the front-end DOM element into PDF and downloading it and operation options for converting the front-end DOM element into PDF and downloading it.

[0039] In some embodiments, the communication network 102 can be any suitable combination of one or more wired and / or wireless networks. For example, the communication network 102 can include any one or more of the following: the Internet, an intranet, a wide area network (WAN), a local area network (LAN), a wireless network, a digital subscriber line (DSL) network, a frame relay network, an asynchronous transfer mode (ATM) network, a virtual private network (VPN) and / or any other suitable communication network. The conversion download client 103 can be connected to the communication network 102 via one or more communication links (e.g., communication link 104), and the communication network 102 can be linked to the conversion download server 101 via one or more communication links (e.g., communication link 105). The communication link can be any communication link suitable for transmitting data between the conversion download client 103 and the conversion download server 101, such as a network link, a dial-up link, a wireless link, a hard-wired link, any other suitable communication link or any suitable combination of such links.

[0040] The conversion download client 103 may include any one or more clients that present an interface related to high-speed access to an ORC external table in an appropriate form for use and operation by a user. In some embodiments, the conversion download client 103 may include any suitable type of device. For example, in some embodiments, the conversion download client 103 may include a mobile device, a tablet computer, a laptop computer, a desktop computer, and / or any other suitable type of client device.

[0041] Although the conversion download server 101 is illustrated as one device, in some embodiments, any suitable number of devices may be used to perform the functions performed by the conversion download server 101. For example, in some embodiments, multiple devices may be used to implement the functions performed by the conversion download server 101. Alternatively, a cloud service may be used to implement the functions of the conversion download server 101.

[0042] Based on the above system, an embodiment of the present application provides a method for converting front-end DOM elements to PDF downloads, which is described below through the following embodiments.

[0043] Figure 2 This is an example diagram of the steps of a method for converting a front-end DOM element to a PDF download according to an embodiment of the present invention. The method for converting a front-end DOM element to a PDF download in this embodiment can be executed on a conversion download server, such as Figure 2 As shown, the method for converting the front-end DOM element to PDF download includes the following steps:

[0044] Step S201: construct a DOM sub-element list by separating DOM elements, and convert the sub-elements in the DOM sub-element list into canvas image list data.

[0045] As an optional example, this embodiment separates the DOM elements to be exported into several blocks of sub-elements, sets the same style name for each block of sub-elements; obtains the DOM sub-elements according to the set style name, and constructs a DOM sub-element list according to the obtained DOM sub-elements. For example, this embodiment adopts the document.querySelectorAll method to obtain the DOM sub-elements, and constructs a DOM sub-element list according to the obtained DOM sub-elements. In actual applications, those skilled in the art may also use other methods to obtain DOM sub-elements, and this embodiment does not limit this. In addition, when separating DOM elements, this embodiment may also separate DOM elements according to the relevance of DOM elements on the page or the size of data.

[0046] After the DOM sub-element list is constructed, the present embodiment loops through the constructed DOM sub-element list and uses html2canvas to convert each sub-element into canvas picture list data. In the present embodiment, html2canvas is a library for writing HTML content into canvas to generate pictures.

[0047] In the present embodiment, DOM is the Document Object Model (DOM for short), which is a standard programming interface for processing extensible markup language recommended by the W3C organization. It is an application program interface (API) that is independent of platform and language. It can dynamically access programs and scripts, update their content, structure and style of www documents (HTML and XML documents are defined by the description part). The document can be further processed, and the result of the processing can be added to the current page. The Chinese name of canvas is "canvas", which is the "container" of all UI components. In a scene, multiple canvas objects can be allowed to exist, and canvases can be "nested" for use. It should be noted that any UI object in the scene must be a "child" of a canvas object. In the present embodiment, querySelectorAll is a document object Document method, which is used to return all elements in the document that match the specified CSS selector and return a node list object.

[0048] Step S202: Create a PDF instance according to the canvas image list data, and obtain the size and position parameters of the image on the PDF according to the size parameters of the created PDF instance and the aspect ratio of the image.

[0049] As an optional example, this embodiment calculates and obtains the maximum image height value and the maximum image width value according to the canvas image list data, and uses the jspdf plug-in to create a pdf instance according to the obtained maximum image height value and the maximum image width value. In practical applications, when the widths of the images are equal, the width of the first image is used as the maximum image width value. In this embodiment, jspdf is an open source library that generates PDFs using JavaScript language.

[0050] As an optional example, this embodiment obtains the width of the created PDF instance by calling the internal getWidth method of the created PDF instance. This embodiment can further improve the size and position accuracy of the image conversion by obtaining the width of the PDF instance.

[0051] This embodiment sets the default gap value for horizontal blank space in PDF, and uses the difference between the width of PDF instance and the default gap value for horizontal blank space in PDF as the width of the image on PDF; the height of each image on PDF is calculated based on the width of the image on PDF and the aspect ratio of the image itself.

[0052] In this embodiment, PDF is a portable file format, also known as "PDF (Portable Document Format) format", which is a file format across operating system platforms. It can encapsulate text, fonts, graphics, images, colors, layouts and parameters related to printing equipment in one file, keep page elements unchanged during network transmission, printing and plate-making output, and can also contain electronic information such as hypertext links, audio and video. It has high integration and security reliability. PDF format files are generated by dedicated "writer" software, and can also be converted by other text and image processing software.

[0053] Step S203: Draw the picture on the PDF according to the size and position parameters of the picture on the PDF, and download the PDF file with the picture drawn on it.

[0054] As an optional example, this embodiment uses the addImage method of jspdf to draw the images on the PDF according to the width of each image on the PDF, the height on the PDF, and the default horizontal margin value of the PDF, and uses the jspdf download save method to download the PDF file.

[0055] The method and system for converting front-end DOM elements to PDF download in the embodiment of the present invention combines the html2canvas library and the jspdf library to separate page elements, set a fixed style name for each part, obtain the list of DOM sub-elements that need to be exported for the page according to the style name, loop through the DOM sub-element list and use the html2canvas library to convert each part of the DOM elements into canvas image list data, then loop through the canvas image list data and use the jspdf library to draw each image on each page of PDF, so that partial or full page PDF content can be downloaded efficiently and clearly, paging will not cause truncation problems, does not rely on other configurations, can be flexibly plug-and-play, has no compatibility issues, and supports all browsers.

[0056] like Figure 3 As shown, the present invention also provides a device, including a processor 310, a communication interface 320, a memory 330 for storing a processor executable computer program, and a communication bus 340. The processor 310, the communication interface 320, and the memory 330 communicate with each other through the communication bus 340. The processor 310 implements the above-mentioned method of converting the front-end DOM element to PDF download by running the executable computer program.

[0057] Among them, the computer program in the memory 330 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0058] The system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, i.e., they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected based on actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.

[0059] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of each embodiment or some parts of the embodiment.

[0060] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims

1. A method for converting front-end DOM elements to PDF download, characterized in that: The method comprises: Build a DOM sub-element list by separating DOM elements, and convert the sub-elements in the DOM sub-element list into canvas image list data; Create a PDF instance based on the canvas image list data, and obtain the size and position parameters of the image on the PDF based on the size parameters of the created PDF instance and the aspect ratio of the image; Draw the image on PDF according to the size and position parameters of the image on PDF, and download the PDF file with the image drawn on it.

2. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: Build a list of DOM child elements by separating DOM elements, including: Separate the DOM elements to be exported into several blocks of sub-elements, and set the same style name for each block of sub-elements; Get the DOM sub-element according to the set style name, and build a DOM sub-element list based on the obtained DOM sub-element.

3. The method for converting front-end DOM elements to PDF download according to claim 2 is characterized in that: The DOM sub-elements are obtained according to the set style name, and the DOM sub-elements list is constructed according to the obtained DOM sub-elements, including: using the document.querySelectorAll method to obtain the DOM sub-elements, and the DOM sub-elements list is constructed according to the obtained DOM sub-elements.

4. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: The method constructs a list of DOM child elements by separating DOM elements, and further comprises separating DOM elements according to the relevance or data size of the DOM elements on the page.

5. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: Convert sub-elements in the DOM sub-element list into canvas image list data, including: looping through the constructed DOM sub-element list, and using html2canvas to convert each sub-element into canvas image list data.

6. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: Create a PDF instance according to the canvas image list data, including: according to the canvas image list data, calculate and obtain the maximum image height value and the maximum image width value, and use the jspdf plug-in to create a PDF instance according to the obtained maximum image height value and the maximum image width value.

7. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: Get the size and position parameters of the image on the PDF according to the size parameters of the created PDF instance and the aspect ratio of the image, including: Get the width of the created PDF instance by calling the internal getWidth method of the created PDF instance; Set the default horizontal margin gap value of PDF, and use the difference between the width of PDF instance and the default horizontal margin gap value of PDF as the width of the image on PDF; The height of each image on PDF is calculated based on the width of the image on PDF and the aspect ratio of the image itself.

8. The method for converting front-end DOM elements to PDF download according to claim 1, characterized in that: Draw the image on PDF according to the size and position parameters of the image on PDF, and download the PDF file with the image drawn on it, including: using jspdf's addImage method to draw the image on PDF according to each image's width on PDF, height on PDF, and default horizontal margin value of PDF, and use jspdf's save method to download the PDF file.

9. A system for converting front-end DOM elements to PDF downloads, characterized in that: The system includes a conversion download server, which is used to: construct a DOM sub-element list by separating DOM elements, and convert the sub-elements in the DOM sub-element list into canvas picture list data; create a PDF instance according to the canvas picture list data, and obtain the size and position parameters of the picture on the PDF according to the size parameters of the created PDF instance and the aspect ratio of the picture; draw the picture on the PDF according to the size and position parameters of the picture on the PDF, and download the PDF file with the picture drawn.

10. A computer device, characterized in that: The computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the method according to any one of claims 1 to 8 when executing the program.

Citation Information

Patent Citations

  • Segmentation printing method and transaction terminal

    CN109634537A

  • Method and device for converting PDF document, terminal and medium

    CN111859865A

  • Webpage-based PDF generation method, device and equipment and storage medium

    CN112036123A

  • File generation method and device, electronic equipment and computer readable storage medium

    CN117407619A

  • Method for converting webpage into pdf capable of being paged

    CN117591766A

Cited By

  • Content truncation and restoration method in process of converting webpage to PDF (Portable Document Format)

    CN119830865A