Method, device and storage medium for generating PDF of page elements based on canvas
By drawing page elements on the canvas canvas and cutting them into pieces, the slow response speed caused by the server-side generation of PDFs is solved, and flexible and efficient PDF generation is achieved.
Patent Information
- Application Number
- CN202510464946.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-15
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2045-04-15
AI Technical Summary
When the server generates PDF files, the system response speed is slow due to its dependence on fixed templates, and it is impossible to flexibly respond to changes in business needs.
By obtaining page elements to draw on the canvas canvas, determining the number of PDF sheets and cutting them in pieces, generating picture data, and finally adding the data to the PDF file stream to generate a PDF file.
The flexibly generates PDFs without preset templates, which improves generation speed and flexibility and improves the system's response efficiency.
Smart Images

Figure CN119990063B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of document processing, and particularly to a method, device, and storage medium for generating a PDF from page elements based on canvas. Background Art
[0002] In the actual application scenario of generating PDF files, it is usually the case that the client sends a network request containing specific parameters to the server. The server queries data based on the received parameters and then generates the corresponding PDF file with the help of a pre-developed template. Since the server's generation of PDF depends on the pre-developed template, in the face of frequently changing business requirements, once it is necessary to adjust the PDF content or format, the template must be re-edited and modified, which reduces the system's response speed and efficiency. Summary of the Invention
[0003] The main objective of this application is to provide a method, device, and storage medium for generating a PDF from page elements based on canvas, aiming to solve the technical problem of slow system response speed when the server generates PDF files due to relying on a fixed template.
[0004] To achieve the above objective, an embodiment of this application provides a method for generating a PDF from page elements based on canvas. The method for generating a PDF from page elements based on canvas includes:
[0005] Obtain the page elements in the area to be exported on the page and draw the page elements on the canvas;
[0006] Obtain the canvas height of the canvas, and determine the required number of PDF pages based on the canvas height, page size, and reserved blank margins;
[0007] Slice and cut the canvas according to the number of PDF pages to generate image data;
[0008] Add the image data to the PDF file stream in sequence to generate a PDF file.
[0009] In one embodiment, before the step of obtaining the page elements in the area to be exported on the page and drawing the page elements on the canvas, it includes:
[0010] Identify the area identifier in the received encoding instruction according to the received encoding instruction;
[0011] Query the DOM structure of the page based on the area identifier, locate the area to be exported, and mark and display the area to be exported.
[0012] In one embodiment, the steps of obtaining the page elements in the area to be exported on the page and drawing the page elements onto the canvas include:
[0013] Traverse the DOM structure of the area to be exported to obtain the page elements within the area to be exported;
[0014] Create a canvas with a corresponding size according to the size information of the area to be exported;
[0015] Draw the page elements onto the canvas according to the layout and style of the page elements in the area to be exported.
[0016] In one embodiment, the steps of obtaining the canvas height of the canvas and determining the required number of PDF pages based on the canvas height, page size, and reserved blank margins include:
[0017] Obtain the canvas height of the canvas based on the attributes of the canvas;
[0018] Determine the available height of the PDF page according to the page size of the PDF page and the reserved blank margins of the page size;
[0019] Calculate the required number of PDF pages based on the canvas height and the available height, where if the calculation result is a decimal, round up.
[0020] In one embodiment, the steps of slicing and cutting the canvas according to the number of PDF pages to generate image data include:
[0021] Based on the number of PDF pages, perform vertical slicing and cutting of the canvas according to the available height of each PDF page to obtain multiple canvas slices;
[0022] Extract the pixel point information of the canvas slices and encode the pixel point information according to the encoding rules of the preset image format to generate image data corresponding to the canvas slices.
[0023] In one embodiment, the step of performing vertical slicing and cutting of the canvas according to the number of PDF pages to obtain multiple canvas slices according to the available height of each PDF page further includes:
[0024] Determine the pre-truncation position of the slicing and cutting and obtain the color data of the pre-truncation position;
[0025] Compare the color data with the background color. If the comparison results are the same, determine the pre-truncation position as the piecewise truncation point and complete the piecewise cutting according to the piecewise truncation point;
[0026] If the comparison results are different, adjust the pre-truncation position and repeat the comparison of the color data at the adjusted position with the background color until the piecewise truncation point where the color data is the same as the background color is found, and complete the piecewise cutting according to the piecewise truncation point.
[0027] In one embodiment, the step of performing piecewise cutting on the canvas according to the number of PDF pages to generate picture data further includes:
[0028] When generating the picture data, associate the picture data corresponding to each piece of the canvas with the page number of the PDF page;
[0029] Store the picture data in sequence according to the order of the page numbers to obtain an ordered set of pictures.
[0030] In one embodiment, the step of sequentially adding the picture data to the PDF file stream to generate a PDF file includes:
[0031] Sequentially obtain the picture data from the set of pictures;
[0032] According to the page size of the PDF page and the reserved blank margin, add the picture data to the PDF file stream one by one, and each picture data corresponds to a PDF page;
[0033] When all the picture data in the set of pictures have been added to the file stream, end the construction of the PDF file stream and generate the PDF file.
[0034] An embodiment of the present application further provides a device for generating a PDF from page elements based on canvas. The device for generating a PDF from page elements based on canvas includes: a memory, a processor, and a computer program stored on the memory and executable on the processor. The computer program is configured to implement the steps of the method for generating a PDF from page elements based on canvas as described above.
[0035] An embodiment of the present application further provides a storage medium. The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the method for generating a PDF from page elements based on canvas as described above are implemented.
[0036] The embodiment of the present application discloses a method for generating a PDF from page elements based on canvas. The method includes obtaining the page elements in the area to be exported on the page and drawing the page elements onto the canvas of the canvas; obtaining the canvas height of the canvas and determining the required number of PDF pages based on the canvas height, page size, and reserved blank margins; slicing and cutting the canvas according to the number of PDF pages to generate image data; and sequentially adding the image data to the PDF file stream to generate a PDF file. By drawing the page elements onto the canvas of the canvas, slicing and cutting them to generate image data, and then adding the image data to the PDF file stream, the present application can flexibly generate a PDF without a preset template, effectively improving the flexibility and response speed of PDF generation. Description of the Drawings
[0037] Figure 1 It is a schematic flowchart of the first embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0038] Figure 2 It is a schematic diagram of the effect of determining the area to be exported in the first embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0039] Figure 3 It is a schematic flowchart of the second embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0040] Figure 4 It is a schematic flowchart of the third embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0041] Figure 5 It is a schematic flowchart of the fourth embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0042] Figure 6 It is a schematic flowchart of the fifth embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0043] Figure 7 It is a schematic flowchart of the sixth embodiment of the method for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application;
[0044] Figure 8 It is a schematic structural diagram of the device for generating a PDF from page elements based on canvas according to the solution of the embodiment of the present application.
[0045] The realization of the purpose, functional features and advantages of this application will be further described in conjunction with the embodiments with reference to the accompanying drawings. Specific embodiments
[0046] It should be understood that the specific embodiments described herein are only used to explain this application and are not used to limit this application.
[0047] In the actual application scenario of generating PDF files, it is usually to send a network request containing specific parameters from the client to the server. The server queries data based on the received parameters, and then generates the corresponding PDF file by means of a pre-developed template. Since the server generates PDF depending on the pre-developed template, in the face of the constantly changing business requirements, once it is necessary to adjust the PDF content or format, it is necessary to re-edit and modify the template, which reduces the response speed and efficiency of the system.
[0048] To solve the above defects existing in the related art, the embodiment of this application proposes a method for generating a PDF from page elements based on canvas. This method obtains the page elements in the area to be exported on the page and draws the page elements on the canvas of the canvas; obtains the canvas height of the canvas of the canvas, and determines the required number of PDF sheets based on the canvas height, page size and reserved blank margins; according to the number of PDF sheets, slice and cut the canvas of the canvas to generate picture data; add the picture data to the PDF file stream in sequence to generate a PDF file. This application draws page elements on the canvas of the canvas and performs slice cutting to generate picture data, and then adds the picture data to the PDF file stream, so that a PDF can be flexibly generated without a preset template, effectively improving the flexibility and response speed of PDF generation.
[0049] It should be noted that the execution subject of this embodiment can be a system for generating a PDF from page elements based on canvas, or a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or a device for generating a PDF from page elements based on canvas that can implement the above functions. The following takes the system for generating a PDF from page elements based on canvas as an example to illustrate this embodiment and the following embodiments.
[0050] The method for generating a PDF from page elements based on canvas in the first embodiment proposed by this application, please refer to Figure 1 , and this method includes steps S10 to S40:
[0051] Step S10: Obtain the page elements in the area to be exported on the page and draw the page elements on the canvas of the canvas.
[0052] A page element refers to various elements that make up the content of a web page, such as text, images, tables, and buttons. These elements exist in the form of DOM (Document Object Model) in a web page. DOM is a structured representation of the web page content, which decomposes the web page content into multiple nodes and objects, enabling users to operate on the web page content through programming languages (such as JavaScript).
[0053] Canvas is an HTML element used for graphic drawing. It provides a user with an area that can be drawn on through a script (usually JavaScript), similar to a virtual canvas. Users can draw various graphics and display content on the canvas.
[0054] In this embodiment, by using the DOM operation function provided by the browser, all page elements within the area to be exported on the page are obtained by locating the identifier of the area to be exported. Then, by using the drawing API of the canvas, the visual representation of the page elements is drawn onto the canvas.
[0055] Exemplarily, for text elements within the area to be exported, the text drawing function of the canvas (such as the fillText method) can be used to set properties such as the font, size, and color of the text, and then draw it to the specified position. For image elements within the area to be exported, the image drawing method of the canvas (such as the drawImage method) can be used to load the image onto the canvas and scale and position it according to requirements.
[0056] In an alternative implementation, the area to be exported is defaulted to the entire page. If the user has specific requirements, each area can be marked during coding, such as using a custom class name or data attribute, to facilitate the user to select the area to be exported. Among them, the marking process can be added to the corresponding HTML elements through CSS or JavaScript code to make different areas distinguishable at the code level.
[0057] In addition, when the user interacts to select the area to be exported, event triggers (such as click events) can be added using JavaScript. When the user clicks on a certain area on the page, the marking information of the corresponding area will be recorded. At the same time, the style of the corresponding area is dynamically modified through DOM operations, making the corresponding area visually presented as a selectable state, such as adding a border, changing the background color, modifying the font color or size, etc.
[0058] Exemplarily, please refer to Figure 2 , Figure 2It is a schematic diagram of the effect of the area to be exported. When the user does not specify a specific area, the area to be exported defaults to covering the entire content of the page. If the user has a need to specify an area, a certain area on the page can be marked through coding, and the corresponding area is set as the area to be exported. At this time, the corresponding area to be exported will be distinguished and displayed on the entire page through a border or background color to ensure that the user can intuitively identify and confirm the area to be exported.
[0059] Step S20: Obtain the canvas height of the canvas, and based on the canvas height, page size, and reserved blank margins, determine the required number of PDF pages.
[0060] After drawing page elements onto the canvas, the elements in the canvas need to be converted into a PDF file. Since PDF files are in a paged form while the canvas is a continuous area, pagination processing needs to be performed on the canvas.
[0061] In an optional implementation, step S20 includes steps S210 - S230:
[0062] Step S210: Based on the attributes of the canvas, obtain the canvas height of the canvas.
[0063] In this embodiment, programming is used to obtain the attribute values of the canvas element, thereby obtaining the size of the canvas in the vertical direction and determining the canvas height of the canvas.
[0064] Step S220: Determine the available height of the PDF page according to the page size of the PDF page and the reserved blank margins of the page size.
[0065] Step S230: Calculate the required number of PDF pages according to the canvas height and the available height, where if the calculation result is a decimal, it is rounded up.
[0066] The page size refers to the size of the finally generated PDF page. The page size of the PDF usually follows the international standard ISO216, such as the common A4 paper size. The reserved blank margins are the blank parts around the PDF page to ensure the aesthetics and readability of the PDF page.
[0067] In this embodiment, first subtract the blank margins reserved above and below the PDF page from the page size of the PDF page to determine the page height actually used for displaying content on the PDF page. Then divide the canvas height of the canvas by the actually available page height to obtain the required number of PDF pages.
[0068] If it cannot be divided evenly, the extra part needs to occupy a separate PDF page, so the result needs to be rounded up.
[0069] Exemplarily, it is determined that the PDF page size is A4 size, the size of A4 paper is 210mm * 297mm, and the reserved blank margin around is 10mm. Then the actual available display area in each PDF page is 190mm * 277mm. And the total height of the obtained canvas is 3450mm, then the required number of PDF pages can be calculated as pageNum = 3450 / 277, and rounding up gives pageNum = 13 pages.
[0070] Step S30: According to the number of PDF pages, slice and cut the canvas to generate picture data.
[0071] Since the canvas is a continuous drawing area and the PDF document is in a paged form, in order to distribute the elements in the canvas to each PDF page, the canvas needs to be sliced according to the number of PDF pages.
[0072] In an optional implementation, step S30 includes steps S310 to S320:
[0073] Step S310: Based on the number of PDF pages, perform vertical slicing and cutting of the canvas according to the available height of each PDF page to obtain a plurality of canvas slices.
[0074] Step S320: Extract the pixel point information of the canvas slices, and encode the pixel point information according to the encoding rules of a preset picture format to generate picture data corresponding to the canvas slices.
[0075] In this embodiment, the pixel point information is the basic unit constituting the canvas slice, and includes information such as color and transparency, which is used to describe the visual content of the canvas slice. Different picture formats have different encoding methods. Therefore, it is necessary to preset to convert the pixel point information into a certain picture format (such as JPEG, PNG, etc.), and then follow the corresponding encoding rules to convert the pixel point information into data of the preset picture format.
[0076] Specifically, first, based on the canvas height of the canvas, according to the required number of PDF pages and the available height of each PDF page, calculate the height of each canvas slice. Then, according to the height of each canvas slice, divide the canvas into multiple parts vertically, and each part corresponds to a PDF page.
[0077] Exemplarily, if the canvas height of the canvas is H and the number of required PDF sheets is n, then the height of each canvas slice is H / n. For each canvas slice, using the image data extraction API of the canvas, the visible content in the canvas slice is extracted, and the canvas slice is converted into image data.
[0078] Next, traverse from the starting position to the ending position of each canvas slice to extract the pixel point information of each canvas slice. According to the encoding rules of the preset image format, the extracted pixel point information is encoded.
[0079] Different image formats have different encoding algorithms. For example, for the JPEG format, an encoding method based on a compression algorithm can be used, while for the PNG format, considering its support for transparency and the characteristics of lossless compression, the pixel point information is converted into binary data of this image format to generate image data corresponding to the canvas slice.
[0080] Step S40: Add the said image data to the PDF file stream in sequence to generate a PDF file.
[0081] The PDF file stream is a way to store and process PDF data in memory or during transmission. It combines various elements in a certain order to gradually form the final PDF file. In this process, the PDF file stream temporarily stores information about various PDF elements, including page layout, images, text, etc., and finally generates a complete PDF file.
[0082] In this embodiment, the obtained image data is arranged in the slicing order of the canvas to ensure that the order of the image data is consistent with the layout order of the original canvas. Then, using the API provided by the PDF generation tool or PDF generation library, the image data is added to the PDF file stream in sequence. Finally, the data stored in the PDF file stream is converted into a storable and viewable PDF file, completing the conversion from page elements to a PDF file, so that the finally generated PDF file can be consistent with the layout and content of the original page elements.
[0083] It should be noted that during the process of adding the image data to the PDF file stream, corresponding page attributes, such as page size, position, resolution, etc., will be set for each image data according to the file format specification of the PDF to ensure that the images can be correctly displayed in the PDF pages.
[0084] Please refer to Figure 3 , for the method of generating a PDF from page elements based on the canvas in the second embodiment proposed by this application, before step S10, it includes steps S101 to S102:
[0085] Step S101: Identify the region identifier in the received encoding instruction according to the encoding instruction.
[0086] Step S102: Based on the region identifier, query the DOM structure of the page, locate the region to be exported, and mark and display the region to be exported.
[0087] The encoding instruction is an instruction including user requirement information, presented in an encoded form, which contains the region identifier specified by the user. The region identifier is a marker used to uniquely identify a certain region in the page. The region identifier can be the ID of an element, a class name, or other custom attributes. Through this region identifier, the region to be exported can be accurately filtered and located in the DOM structure of the page.
[0088] The DOM structure is a hierarchical representation of the web page content, which allows users to access and manipulate page elements programmatically. In the DOM structure, each node represents an HTML element, including information such as the attributes of the element and its child elements.
[0089] After receiving the encoding instruction, identify the region identifier in the encoding instruction, and then locate the region to be exported by traversing the DOM tree or using DOM query methods. To confirm the selected region to the user, the system will mark and display the selected region. The region to be exported can be highlighted by changing the border color of the region, adding a shadow, or highlighting it. Specifically, the marking and display of the region to be exported can be achieved by modifying the style attributes of the DOM element, such as setting CSS properties such as border, box-shadow, or background-color.
[0090] Please refer to Figure 4 , the method for generating a PDF from page elements based on canvas proposed in the third embodiment of the present application, step S10 includes steps S110 to S130:
[0091] Step S110: Traverse the DOM structure of the region to be exported, and obtain the page elements within the region to be exported.
[0092] In this embodiment, according to the region identifier of the region to be exported that has been determined, traverse the DOM structure of the region to be exported through the DOM operation function of the programming language, check each node in turn, and obtain DOM objects to extract the page elements within the region to be exported.
[0093] Step S120: Create a canvas drawing board with a corresponding size according to the size information of the region to be exported.
[0094] In this embodiment, according to the obtained size information of the area to be exported, a canvas canvas with a corresponding size is created by using the creation function of the canvas element, so as to ensure that the canvas canvas can completely cover the area to be exported and provide sufficient space for the drawing of page elements.
[0095] Step S130: Draw the page element onto the canvas canvas according to the layout and style of the page element in the area to be exported.
[0096] For different types of page elements, corresponding canvas drawing operations are used. According to the style attributes of different types of page elements in the DOM structure, the page elements are drawn onto the canvas canvas in the correct shape, position and size, ensuring that the drawn page elements can maintain the original layout and style in the original page on the canvas canvas.
[0097] Please refer to Figure 5 , based on the above embodiments of the present application, for the method of generating a PDF from page elements based on canvas in the fourth embodiment proposed by the present application, step S310 further includes steps S311 to S313:
[0098] Step S311: Determine the pre-truncation position for slice cutting and obtain the color data at the pre-truncation position.
[0099] When the canvas canvas is vertically sliced according to the available height of the PDF page, the situation of truncating element content may occur. To avoid truncating part of the content of an element onto different pages, it is necessary to determine whether the current pre-truncation position is the final slice truncation point according to the color at the pre-truncation position.
[0100] The pre-truncation position is calculated based on the canvas height of the canvas canvas, the determined number of PDF pages and the available height of each PDF page to obtain the slice height of each canvas slice, thereby obtaining the pre-truncation position for slice cutting.
[0101] The color data refers to the color information of the pixel points at the pre-truncation position, including RGB values, transparency, etc. By obtaining the color data at the pre-truncation position, it can be determined whether this position is suitable as the slice truncation point.
[0102] The background color refers to the background color of the page or the canvas canvas.
[0103] Step S312: Compare the color data with the background color. If the comparison result is consistent, determine the pre-truncation position as the slice truncation point and complete the slice cutting according to the slice truncation point.
[0104] When determining whether the pre-truncation position is the final shard truncation point, the background color is used as a reference standard for comparison with the color data at the pre-truncation position. If the color data at the pre-truncation position is the same as the background color, such as the same RGB values, it indicates that the pre-truncation position is in the blank area between page elements. Therefore, this pre-truncation position can be used as the final shard truncation point, and shard cutting is completed.
[0105] Step S313: If the comparison result is inconsistent, adjust the pre-truncation position and repeat the comparison of the color data at the adjusted position with the background color until the shard truncation point with the same color data as the background color is found, and complete shard cutting according to the shard truncation point.
[0106] If the comparison result is inconsistent, it means that the pre-truncation position may be in the middle of a certain page element. Cutting the shard according to this pre-truncation position will truncate a complete page element. Therefore, it is necessary to adjust the pre-truncation position. The adjustment operation can be to finely adjust the pre-truncation position up or down by a certain number of pixel units, then obtain the color data at the adjusted position again, and repeat the comparison operation with the background color.
[0107] By continuously adjusting the pre-truncation position and comparing the color of the adjusted position with the background color until a position with the same color data as the background color is found, ensure that this position is suitable as the shard truncation point. Finally, use the adjusted pre-truncation position as the shard truncation point to complete shard cutting and cut the adjusted canvas into canvas shards.
[0108] In this embodiment, through color comparison and position adjustment, it is ensured that each canvas shard after shard cutting is relatively complete in content, avoiding unreasonable truncation of page elements, thereby improving the quality and readability of the generated PDF file.
[0109] Please refer to Figure 6 , in the method for generating a PDF from page elements based on canvas according to the fifth embodiment proposed in this application, step S30 further includes steps S330 to S340:
[0110] Step S330: When generating the picture data, associate the picture data corresponding to each canvas shard with the page number of the PDF page.
[0111] During the process of slicing the canvas and converting it into image data, multiple image data are generated. However, without explicit order management, these image data will cause the final PDF page order to be chaotic. To ensure the correct arrangement of the page order and content in the finally generated PDF file, these image data need to be organized in an orderly manner. Therefore, numbers are assigned to each PDF page, representing the order of the PDF pages in the PDF file, which facilitates the differentiation and sorting of different pages.
[0112] Specifically, when generating the image data corresponding to each canvas slice, according to the number of PDF pages and the position of the current canvas slice in the entire canvas slices, the page serial number of the corresponding PDF page is assigned to each image data. For example, when processing the first canvas slice, the image data corresponding to this canvas slice is assigned to the PDF page with page serial number 1, and the image data corresponding to the second canvas slice is assigned to the PDF page with page serial number 2, and so on.
[0113] Step S340: Store the image data in sequence according to the order of the page serial numbers to obtain an ordered set of images.
[0114] According to the order of the page serial numbers of the PDF pages, the generated image data are stored in sequence to obtain an ordered set of images. During the storage process, data structures such as arrays or lists are used to store the image data according to the size of the page serial numbers. When a new image data is generated, according to the page serial number associated with this image data, it is inserted into the corresponding position to ensure that the stored set of images is arranged in ascending or descending order of the page serial numbers.
[0115] Please refer to Figure 7 , the method for generating a PDF from page elements based on canvas in the sixth embodiment proposed in this application, step S40 includes steps S410 to S430:
[0116] Step S410: Obtain the image data from the set of images in sequence.
[0117] In the foregoing embodiment, the canvas slices of the canvas have been converted into image data and stored in the set of images. Then, the image data needs to be added to the PDF file. Since PDF is a paged file, each image data needs to be added as a separate PDF page.
[0118] Step S420: Add the image data to the PDF file stream one by one according to the page size of the PDF page and the reserved blank margin, and each image data corresponds to a PDF page.
[0119] In this embodiment, first, according to the page size of the PDF page and the reserved blank margins, calculate the actual available space of the image data in the PDF page, so as to perform appropriate layout and scaling on the image data to ensure that the images are reasonably displayed on the page.
[0120] Next, use the API provided by the PDF generation tool or PDF generation library to add the image data to the PDF file stream. During the addition process, according to the page size of the PDF page, the reserved blank margins, and the size of the image data, adjust the position and size of the image data so that the image data adapts to the display range of the PDF page.
[0121] Exemplarily, call the function of adding an image in the PDF generation library, take the image data as a parameter, and pass information such as the page size of the PDF page and the reserved blank margins, so that the image data can be correctly displayed in the PDF page.
[0122] Step S430: When all the image data in the image set have been added to the file stream, end the construction of the PDF file stream and generate the PDF file.
[0123] In this embodiment, continuously take out the image data from the image set and add it to the PDF file stream until there is no remaining image data in the image set. After all the image data has been added, trigger the file generation operation to convert the data stored in the PDF file stream into the final PDF file.
[0124] This embodiment uses the storage structure and the PDF generation tool to orderly add the image data generated by slicing the canvas to the PDF file stream, and finally generates a complete PDF file, realizing the conversion from image data to PDF file, and at the same time ensuring that the page layout and content display of the generated PDF file are consistent with the original page.
[0125] An embodiment of the present application provides a device for generating a PDF from page elements based on canvas. The device for generating a PDF from page elements based on canvas includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for generating a PDF from page elements based on canvas in the first embodiment above.
[0126] Next, refer to Figure 8, which shows a schematic structural diagram of a device suitable for implementing the method of generating a PDF from page elements based on canvas according to an embodiment of the present application. The device for generating a PDF from page elements based on canvas in the embodiments of the present application may include various hardware and software components for implementing the method of generating a PDF from page elements based on canvas. Figure 8 The shown device for generating a PDF from page elements based on canvas is only an example and should not impose any limitation on the functions and usage scope of the embodiments of the present application.
[0127] As Figure 8 shown, the device for generating a PDF from page elements based on canvas may include a processing device 1001 (such as a central processing unit, a graphics processing unit, etc.), which may perform various appropriate actions and processes according to the program stored in the read-only memory (ROM: Read Only Memory) 1002 or the program loaded from the storage device 1003 into the random access memory (RAM: Random Access Memory) 1004. In the RAM 1004, various programs and data required for the operation of the device for generating a PDF from page elements based on canvas are also stored. The processing device 1001, the ROM 1002, and the RAM 1004 are connected to each other through a bus 1005. The input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems may be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touchpad, a keyboard, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 may allow the device for generating a PDF from page elements based on canvas to communicate with other devices wirelessly or wiredly to exchange data. Although the figure shows a device for generating a PDF from page elements based on canvas having various systems, it should be understood that it is not required to implement or have all the shown systems. Instead, more or fewer systems may be implemented or had.
[0128] In particular, according to the embodiments disclosed in the present application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium. The computer program contains program codes for executing the methods shown in the flowcharts. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by a processing device 1001, the above-mentioned functions defined in the methods of the embodiments disclosed in the present application are executed.
[0129] The device for generating a PDF from page elements based on canvas provided by the present application adopts the method for generating a PDF from page elements based on canvas in the above-mentioned embodiment, and can solve the technical problem that the system response speed is slow due to relying on a fixed template when the server generates a PDF file. Compared with the prior art, the beneficial effects of the device for generating a PDF from page elements based on canvas provided by the present application are the same as those of the method for generating a PDF from page elements based on canvas provided in the above-mentioned embodiment, and other technical features in the device for generating a PDF from page elements based on canvas are the same as the features disclosed in the method of the previous embodiment, and will not be elaborated here.
[0130] It should be understood that each part disclosed in the present application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in a suitable manner in any one or more embodiments or examples.
[0131] As described above, only the specific embodiments of the present application are provided, but the protection scope of the present application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present application, and all should be covered by the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
[0132] The embodiments of the present application provide a computer-readable storage medium, on which there are computer-readable program instructions (i.e., computer programs) for executing the method for generating a PDF from page elements based on canvas in the above-mentioned embodiment.
[0133] The computer-readable storage medium provided by this application can be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or components, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections with one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM) or flash memory, optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the above. In this embodiment, the computer-readable storage medium can be any tangible medium that contains or stores a program, and this program can be used by or in combination with an instruction execution system, device, or component. The program code contained on the computer-readable storage medium can be transmitted by any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.
[0134] The above computer-readable storage medium can be included in a device that generates PDF from page elements based on canvas; it can also exist separately and not be assembled into a device that generates PDF from page elements based on canvas.
[0135] The above computer-readable storage medium carries one or more programs. When the one or more programs are executed by a device that generates PDF from page elements based on canvas, the device that generates PDF from page elements based on canvas is enabled to: obtain the page elements in the area to be exported on the page and draw the page elements on the canvas; obtain the canvas height of the canvas and determine the required number of PDF pages based on the canvas height, page size, and reserved blank margins; slice and cut the canvas according to the number of PDF pages to generate image data; and sequentially add the image data to the PDF file stream to generate a PDF file.
[0136] Computer program code for performing the operations of this application can be written in one or more programming languages or combinations thereof. The above-mentioned programming languages include object-oriented programming languages such as Java, Smalltalk, C++, and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, executed as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any kind of network, including a local area network (LAN: Local Area Network) or a wide area network (WAN: Wide Area Network), or it can be connected to an external computer (for example, by connecting through the Internet using an Internet service provider).
[0137] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in the flowchart or block diagram may represent a module, a program segment, or a part of code that contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks shown may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, and the combination of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.
[0138] The modules described in the embodiments of this application can be implemented in software or in hardware. Among them, the name of the module does not constitute a limitation on the unit itself in some cases.
[0139] The readable storage medium provided in this application is a computer-readable storage medium. The computer-readable storage medium stores computer-readable program instructions (i.e., computer programs) for performing the above-mentioned method of generating PDF from page elements based on canvas, and can solve the technical problem that the system response speed is slow due to relying on a fixed template when the server generates a PDF file. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the method of generating PDF from page elements based on canvas provided in the above embodiments, and will not be elaborated here.
[0140] An embodiment of the present application provides a computer program product, including a computer program, and when the computer program is executed by a processor, it implements the steps of the method for generating a PDF from page elements based on canvas as described above.
[0141] The computer program product provided by the present application can solve the technical problem that the system response speed is slow due to relying on a fixed template when the server generates a PDF file. Compared with the prior art, the beneficial effects of the computer program product provided by the embodiment of the present application are the same as those of the method for generating a PDF from page elements based on canvas provided by the above embodiment, and will not be elaborated here.
[0142] The above are only the preferred embodiments of the present application, and do not limit the patent scope of the present application accordingly. Any equivalent structural or equivalent process transformation made by using the content of the specification and drawings of the present application, or directly or indirectly applied in other related technical fields, shall be equally included in the patent scope of the present application.
[0143] It should be noted that in this text, the term "including", "comprising" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or system. Without further limitation, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article or system including that element.
[0144] Through the description of the above embodiments, those skilled in the art can clearly understand that the above embodiment method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware, but in many cases, the former is a better implementation method.
[0145] The above are only the preferred embodiments of the present application, and do not limit the patent scope of the present application accordingly. Any equivalent structural or equivalent process transformation made by using the content of the specification and drawings of the present application, or directly or indirectly applied in other related technical fields, shall be equally included in the patent protection scope of the present application.
Claims
1. A method for generating a PDF from page elements based on canvas, characterized in that, The method for generating a PDF from page elements based on canvas includes: Obtain the page elements in the area to be exported on the page, and draw the page elements on the canvas; Obtain the canvas height of the canvas, and determine the required number of PDF pages based on the canvas height, page size, and reserved blank margins; Based on the number of PDF pages, perform vertical slicing and cutting of the canvas according to the available height of each PDF page to obtain multiple canvas slices; Extract the pixel point information of the canvas slices, and encode the pixel point information according to the encoding rules of the preset image format to generate image data corresponding to the canvas slices; Add the image data to the PDF file stream in sequence to generate a PDF file; The step of performing vertical slicing and cutting of the canvas according to the available height of each PDF page based on the number of PDF pages to obtain multiple canvas slices further includes: Determine the pre-truncation position of the slicing and cutting, and obtain the color data at the pre-truncation position; Compare the color data with the background color. If the comparison result is consistent, determine the pre-truncation position as the slicing truncation point, and complete the slicing and cutting according to the slicing truncation point; If the comparison result is inconsistent, adjust the pre-truncation position upward according to the preset pixel unit, and repeat the comparison of the color data at the adjusted position with the background color until the slicing truncation point where the color data is consistent with the background color is found, and complete the slicing and cutting according to the slicing truncation point.
2. The method for generating a PDF from page elements based on canvas according to claim 1, characterized in that, Before the step of obtaining the page elements in the area to be exported on the page and drawing the page elements on the canvas, it includes: According to the received encoding instruction, identify the area identifier in the encoding instruction; Based on the area identifier, query the DOM structure of the page, locate the area to be exported, and mark and display the area to be exported.
3. The method for generating a PDF from page elements based on canvas according to claim 1, wherein, The step of obtaining the page elements in the area to be exported on the page and drawing the page elements on the canvas includes: Traverse the DOM structure of the area to be exported to obtain the page elements in the area to be exported; Create a canvas with the corresponding size according to the size information of the area to be exported; Draw the page elements on the canvas according to the layout and style of the page elements in the area to be exported.
4. The method for generating a PDF of page elements based on canvas according to claim 1, wherein The step of obtaining the canvas height of the canvas and determining the required number of PDF pages based on the canvas height, page size, and reserved blank margins includes: Based on the attributes of the canvas, obtain the canvas height of the canvas; Determine the available height of the PDF page according to the page size of the PDF page and the reserved blank margin of the page size; Calculate the required number of PDF pages according to the canvas height and the available height. If the calculation result is a decimal, round up.
5. The method for generating a PDF from page elements based on canvas according to claim 1, wherein The step of slicing and cutting the canvas according to the number of PDF pages to generate picture data further includes: When generating the picture data, associating the picture data corresponding to each sliced canvas with the page number of the PDF page; Storing the picture data in sequence according to the order of the page numbers to obtain an ordered set of pictures.
6. The method for generating a PDF of page elements based on canvas according to claim 1, wherein The step of generating a PDF file by sequentially adding the picture data to a PDF file stream includes: Sequentially obtaining the picture data from the set of pictures; According to the page size of the PDF page and the reserved blank margin, adding the picture data to the PDF file stream one by one, with each picture data corresponding to a PDF page; When all the picture data in the set of pictures have been added to the file stream, stop constructing the PDF file stream and generate the PDF file.
7. A device for generating a PDF of page elements based on canvas, characterized in that, The device for generating a PDF from page elements based on canvas includes: A memory, a processor, and a computer program stored on the memory and executable on the processor, the computer program being configured to implement the steps of the method for generating a PDF from page elements based on canvas according to any one of claims 1 to 6.
8. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, it implements the steps of the method for generating a PDF from page elements based on canvas according to any one of claims 1 to 6.
Citation Information
Patent Citations
PDF format data exporting method and device, electronic equipment and readable storage medium
CN114004209A
Implementation method for converting front-end page into PDF (Portable Document Format) and improving definition of PDF based on canvas
CN118428323A