A method for exporting PDF files based on Puppeteer

Through the PDF file export method based on Puppeteer, the layout confusion and style distortion problems during the export of wrong questions are solved, and high-quality and efficient PDF file generation is achieved, improving user experience and system ease of use.

CN118132017BActive Publication Date: 2025-07-08BEIJING HOPE ONLINE SUBJECT TRAINING SCHOOL
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410398263.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-04-03
Publication Date
2025-07-08
Estimated Expiration
2044-04-03

AI Technical Summary

Technical Problem

When the prior art exports multiple wrong questions data into PDF files, problems such as layout disorder and style distortion are prone to occur.

Method used

The PDF file export method based on Puppeteer is adopted, and the page module of the wrong question is used to work together through the collaborative work of the wrong question page module, the wrong question server, the Koa service middleware, the PDF export service module and the question template page module, and the Puppeteer rendering engine is used to simulate the real browser environment and generate high-quality PDF files.

Benefits of technology

High-quality and efficient PDF printing is achieved, improving user experience, ensuring layout and style accuracy, and providing ease of use and security.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118132017B_ABST
    Figure CN118132017B_ABST
Patent Text Reader

Abstract

The present invention provides a method for exporting PDF files based on Puppeteer, which includes: the wrong-question notebook page module sends the test question data to be exported as a PDF file to the wrong-question server; the wrong-question server sends the test paper ID to the wrong-question notebook page module; the wrong-question notebook page module sends a PDF export request to the PDF export service module, and the PDF export request includes the test paper ID, the question template type, and the printing style parameters; the Koa service middleware verifies the test paper ID in the PDF export request, and after passing the verification, PDF printing is performed through the PDF export service module. The method for exporting PDF files based on Puppeteer provided by the present invention realizes high-quality and efficient PDF printing through the cooperation of the wrong-question notebook page module, the wrong-question server, the Koa service middleware, the PDF export service module, and the question template page module, improving the user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of PDF file export, and particularly relates to a method for exporting PDF files based on Puppeteer. Background Art

[0002] Currently, multiple wrong-question data of students on the APP side often need to be exported as PDF files for printing or saving. In the prior art, when exporting multiple wrong-question data as PDF files, problems such as disordered layout and distorted styles are likely to occur. Summary of the Invention

[0003] Aiming at the defects existing in the prior art, the present invention provides a method for exporting PDF files based on Puppeteer, which can effectively solve the above problems.

[0004] The technical solution adopted by the present invention is as follows:

[0005] The present invention provides a method for exporting PDF files based on Puppeteer, including the following steps:

[0006] Step S1, the mobile terminal has a wrong-question book page module; the wrong-question book page module sends the test question data to be exported as a PDF file to the wrong-question server.

[0007] Step S2, the wrong-question server generates a test paper ID according to the received test question data and sends the test paper ID to the wrong-question book page module.

[0008] Step S3, the wrong-question book page module sends a PDF export request to the PDF export service module, and the PDF export request includes a test paper ID, a question template type, and a printing style parameter.

[0009] Step S4, the PDF export request is intercepted by the Koa service middleware, and the Koa service middleware parses the PDF export request to obtain the test paper ID; then, the Koa service middleware sends a request to the wrong-question server to obtain the test paper ID generated at the latest time and compares it with the test paper ID parsed from the PDF export request. If they are the same, the test paper validity verification passes, and step S5 is executed; if they are different, the verification fails, and a notice of refusal to print is returned to the wrong-question book page module.

[0010] Step S5, the Koa service middleware sends the PDF export request to the PDF export service module.

[0011] Step S6, the PDF export service module parses the test paper ID, question template type, and printing style parameters in the PDF export request, and sends the test paper ID to the question template page module corresponding to the question template type;

[0012] Step S7, the question template page module sends a request to the wrong question server to obtain all the question data corresponding to the test paper ID, and receives all the question data returned by the wrong question server;

[0013] Step S8, the question template page renders all the received question data according to the question template style to obtain a question data page, and returns it to the PDF export service module;

[0014] Step S9, the PDF export service module performs PDF export printing on the question data page according to the printing style parameters to obtain a PDF file stream, and returns it to the Koa service middleware;

[0015] Step S10, the Koa service middleware returns the PDF file stream to the wrong question book page module to complete the export of the PDF file.

[0016] Preferably, the question template type corresponds to a layout style.

[0017] Preferably, the printing style parameters include header, footer, and watermark parameters.

[0018] Preferably, the PDF export service module is a PDF export service module based on puppeteer.

[0019] Preferably, Step S9 is specifically:

[0020] The PDF export service module processes the question data page according to the printing style parameters to generate an HTML page, and then exports the generated HTML page as a PDF file stream through the puppeteer rendering engine.

[0021] Preferably, the puppeteer rendering engine runs in headless browser mode, simulates the environment of a real browser, applies the question data page to a web template, and processes the question data page in the web template based on the printing style parameters to obtain an HTML page that is exactly the same as the page displayed by the user on the desktop browser.

[0022] A puppeteer-based pdf file export method provided by the present invention has the following advantages:

[0023] A method for exporting PDF files based on Puppeteer provided by the present invention realizes high-quality and efficient PDF printing through the collaboration of a wrong-question notebook page module, a wrong-question server, a Koa service middleware, a PDF export service module, and a question template page module, improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1 It is a schematic flowchart of the method for exporting PDF files based on Puppeteer provided by the present invention;

[0025] Figure 2 It is a timing diagram of the method for exporting PDF files based on Puppeteer provided by the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0026] In order to make the technical problems, technical solutions, and beneficial effects solved by the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0027] The present invention provides a method for exporting PDF files based on Puppeteer, which has the following advantages:

[0028] 1) User-friendly: By providing an intuitive and easy-to-use interface, non-professional developers can also use it easily, greatly improving the usability of the system.

[0029] 2) Powerful functions: The data processing module is not only responsible for processing the test question data selected by the user, but also can set the layout style, generate answer analysis, knowledge point labels, etc. according to the user's needs.

[0030] 3) Excellent rendering effect: The Puppeteer technology is used to perfectly simulate the browser rendering effect, and the expected page is generated according to the settings obtained by the data processing module. This solves the problems of layout disorder and style distortion in the traditional method.

[0031] 4) High output quality: The PDF export module can quickly convert the page rendered by Puppeteer into a PDF file and output it. This step ensures that the output file meets the requirements of high-quality printing.

[0032] 5) Improve work efficiency: Since the Puppeteer technology is used to quickly generate PDF documents, the whole process is more efficient than the traditional method.

[0033] 6) System management and monitoring functions: This part allows administrators to effectively monitor and manage the entire PDF export service, including error detection, performance optimization, etc.

[0034] 7) Strong scalability: Development based on the Node.js environment makes it simple and easy to expand service functions.

[0035] A method for exporting PDF files based on puppeteer provided by the present invention realizes high-quality and efficient PDF printing and improves the user experience through the cooperation of the wrong-question notebook page module, the wrong-question server, the Koa service middleware, the PDF export service module, and the question template page module.

[0036] Refer to Figure 1 and Figure 2 , the present invention provides a method for exporting PDF files based on puppeteer, including the following steps:

[0037] Step S1, the mobile terminal has a wrong-question notebook page module; the wrong-question notebook page module sends the test question data that needs to be exported as a PDF file to the wrong-question server;

[0038] Step S2, the wrong-question server generates a test paper id according to the received test question data and sends the test paper id to the wrong-question notebook page module;

[0039] Step S3, the wrong-question notebook page module sends a PDF export request to the PDF export service module. The PDF export request includes a test paper id, a question template type, and printing style parameters; among them, the question template type corresponds to a layout style, and the printing style parameters include but are not limited to header, footer, and watermark parameters.

[0040] Step S4, the PDF export request is intercepted by the Koa service middleware. The Koa service middleware parses the PDF export request to obtain the test paper id; then, the Koa service middleware sends a request to the wrong-question server to obtain the test paper id generated at the most recent time and compares it with the test paper id parsed from the PDF export request. If they are the same, the test paper validity verification passes, and step S5 is executed; if they are different, the verification fails, and a notice of refusal to print is returned to the wrong-question notebook page module;

[0041] Step S5, the Koa service middleware sends the PDF export request to the PDF export service module;

[0042] Step S6, the PDF export service module parses the test paper id, the question template type, and the printing style parameters in the PDF export request and sends the test paper id to the question template page module corresponding to the question template type;

[0043] Step S7, the question template page module sends a request to the wrong question server to obtain all the question data corresponding to the test paper id, and receives all the question data returned by the wrong question server;

[0044] Step S8, the question template page renders all the received question data according to the question template style to obtain a question data page, and returns it to the PDF export service module;

[0045] Step S9, the PDF export service module performs PDF export printing on the question data page according to the printing style parameters to obtain a PDF file stream, and returns it to the Koa service middleware;

[0046] In the present invention, the PDF export service module is a PDF export service module based on puppeteer. This step specifically is: the PDF export service module processes the question data page according to the printing style parameters to generate an HTML page, and then exports the generated HTML page as a PDF file stream through the puppeteer rendering engine. The puppeteer rendering engine runs in a headless browser mode, simulates the environment of a real browser, applies the question data page to the web template, and processes the question data page in the web template based on the printing style parameters to obtain an HTML page that is exactly the same as the page displayed by the user side in the desktop browser.

[0047] Step S10, the Koa service middleware returns the PDF file stream to the wrong question book page module to complete the export of the PDF file.

[0048] A puppeteer-based pdf file export method provided by the present invention, which is used for a puppeteer-based pdf file export system, is an efficient test paper export tool, provides a variety of question layout styles for selection, can typeset according to the selected question data by the user, and outputs the result as a PDF file meeting the requirements of high-quality printing.

[0049] This solution can perfectly simulate the browser rendering effect, customize the layout and style of the question page, support the setting of information such as headers, footers, and watermarks, and also combines an advanced HTML-to-PDF conversion algorithm to optimize the rendering performance and output quality. Especially when processing elements such as complex document structures, charts, and mathematical formulas, it can ensure a high-fidelity conversion.

[0050] At the same time, it also provides a rich variety of question types, and also includes a highly customizable question answer analysis and knowledge point tagging function, which helps tutoring teachers easily produce high-quality questions.

[0051] In the present invention, the deployment process of the PDF export service is optimized. Using the puppeteer image as the base image, by encapsulating the service source code into a Docker container image, the differences between different operating environments are effectively eliminated, greatly simplifying the deployment process. In addition, using Kubernetes (k8s) as the container orchestration system not only enables the rapid deployment and automated management of the service, but also allows the service to be flexibly horizontally scaled to gracefully handle high concurrency situations. This deployment strategy combining Docker and Kubernetes provides an efficient, stable, and scalable solution for the PDF export service. Among them, the docker container image building process is as follows: 1. Create a Dockerfile; 2. Build a Docker image; 3. Push the built Docker image to the image repository; 4. Use Kubernetes as the container orchestration tool to deploy and manage the service.

[0052] The Puppeteer rendering engine utilizes the Puppeteer library, which provides a rich set of APIs to manipulate the Chrome or Chromium browser. This rendering engine runs in headless mode, which means it can be executed in the background without a graphical user interface. Therefore, it can run very efficiently in a server environment and is suitable for various scenarios such as automated testing, web page screenshotting, and PDF generation. The core advantage of the Puppeteer rendering engine is its ability to simulate the environment of a real browser, including executing JavaScript, loading CSS, and other resources, thus ensuring that the page rendering effect is consistent with what the user sees in a desktop browser. This is a huge advantage for developers as it allows testing and validating the display effect of web pages in different environments, ensuring cross-platform consistency and responsiveness.

[0053] Combined with the data processing module, the Puppeteer rendering engine can dynamically generate content based on the provided data. The data processing module is usually responsible for obtaining data from databases, APIs, or other data sources, and then processing this data according to predefined templates or logic. The Puppeteer rendering engine receives the processed data and applies it to the web page template to generate the page that the end user will see.

[0054] In the prior art, the PDF export service based on Puppeteer allows users to convert web page content into PDF format through a web interface. However, this service may face the risk of malicious calls, such as unauthorized PDF generation requests, which may lead to service overload or data leakage. To solve the above problems, the present invention proposes a method for verifying the validity of the test paper ID using the Koa service middleware to ensure the legality and security of the PDF export operation. In an embodiment of the present invention, the PDF export service integrates the Koa framework as the basis of its web service. Koa is a new web framework designed to handle web applications and services more gracefully. Utilizing the middleware mechanism of Koa, the present invention implements a method for verifying the validity of the test paper ID.

[0055] Before the PDF export request enters the Koa service, it is first intercepted by a specially designed middleware. This middleware is responsible for verifying the test paper id included in each export request. The test paper id is a predefined unique identifier used to confirm whether the request comes from an authorized source. In this way, the Koa service middleware provides a layer of security protection for the PDF export service, effectively preventing unauthorized or malicious export operations and ensuring the stability of the service and the security of the data.

[0056] The following introduces a usage example:

[0057] 1. The student selects the wrong question data and the question template type to be printed in the mobile app; among them, the wrong question data includes the wrong question, or the question answer, or a combination of the wrong question and the question answer. The question template type is the page layout method for printing. Then click the PDF print button;

[0058] 2. The app end calls the wrong question server to obtain the test paper id (valid for 5 minutes) generated by the wrong question server, and then forms a PDF export request with the test paper id, the question template type, and the print style parameters, and sends it to the PDF export service module;

[0059] 3. The PDF export service module is intercepted by the Koa service middleware, and the Koa service middleware verifies the validity of the test paper id. If it is valid, the process continues; if it is invalid, the process is directly terminated;

[0060] 4. The PDF export service module uses puppeteer to open the corresponding question template page module, calls the wrong question server to obtain the wrong question data selected by the student through the test paper id for rendering, and adds a watermark to the page by injecting js;

[0061] 5. The puppeteer calls the printing function of Chrome, customizes the header and footer through parameter configuration, and generates the buffer of the PDF file;

[0062] 6. Return the buffer of the PDF file to the app side to achieve the export of the PDF file.

[0063] A method for exporting PDF files based on puppeteer provided by the present invention has the following characteristics:

[0064] 1) Provide multiple question layout styles for selection, and can perform personalized layout according to the question data selected by the user.

[0065] 2) Support the setting of information such as headers, footers, watermarks, etc., and provide rich question types as well as highly customizable answer analysis and knowledge point label functions.

[0066] 3) Perfectly simulate the browser rendering effect, and customize the question page layout and style.

[0067] 4) Provide an easy-to-use and friendly interface, so that non-professional developers can also use it easily.

[0068] 5) Ensure that the output is a PDF file that meets the requirements of high-quality printing.

[0069] 6) Quickly generate PDF documents through Puppeteer technology to improve work efficiency.

[0070] 7) Developed based on the Node.js environment, making it simple and easy to expand service functions.

[0071] 8) Provide a layer of security protection to prevent unauthorized or malicious export operations, and ensure the stability of the service and the security of data.

[0072] A method for exporting PDF files based on puppeteer provided by the present invention has the following advantages:

[0073] 1) User-friendly interface design: By providing an intuitive and easy-to-use interface, non-professional developers can also use it easily, greatly improving the usability of the system.

[0074] 2) The data processing module supports multiple printing contents and printing styles: It not only supports the question data selected by the user, but also can set the layout style, generate answer analysis and knowledge point labels, etc. according to the user's needs.

[0075] 3) Puppeteer rendering engine: Perfectly simulate the browser rendering effect through Puppeteer technology, and generate the expected page according to the settings obtained by the data processing module, solving the problems of layout disorder and style distortion existing in the traditional method.

[0076] 4) PDF Export Service Module: This module can quickly convert the page rendered by Puppeteer into a PDF file and output it. This step ensures that the output file meets the requirements of high-quality printing and improves work efficiency.

[0077] 5) System Management and Monitoring Function: This part allows administrators to effectively monitor and manage the entire PDF export service, including error detection and performance optimization, etc.

[0078] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.

Claims

1. A method for exporting PDF files based on Puppeteer, characterized in that, It includes the following steps: Step S1, the mobile terminal has a wrong-question book page module; the wrong-question book page module sends the test question data that needs to be exported as a PDF file to the wrong-question server; Step S2, the wrong-question server generates a test paper id according to the received test question data according to the set rules, and sends the test paper id to the wrong-question book page module; Step S3, the wrong-question book page module sends a PDF export request to the PDF export service module, and the PDF export request has a test paper id, a question template type, and print style parameters; Step S4, the PDF export request is intercepted by the Koa service middleware, and the Koa service middleware parses the PDF export request to obtain the test paper id; then, the Koa service middleware sends a request to the wrong-question server to obtain the test paper id generated at the nearest time, and compares it with the test paper id parsed from the PDF export request. If they are the same, the test paper validity verification passes, and step S5 is executed; if they are different, the verification fails, and a notice of refusal to print is returned to the wrong-question book page module; Step S5, the Koa service middleware sends the PDF export request to the PDF export service module; Step S6, the PDF export service module parses the test paper id, question template type, and print style parameters in the PDF export request, and sends the test paper id to the question template page module corresponding to the question template type; Step S7, the question template page module sends a request to the wrong-question server to obtain all the test question data corresponding to the test paper id, and receives all the test question data returned by the wrong-question server; Step S8, the question template page renders all the received test question data according to the question template style to obtain a test question data page, and returns it to the PDF export service module; Step S9, the PDF export service module performs PDF export printing on the test question data page according to the print style parameters to obtain a PDF file stream, and returns it to the Koa service middleware; Step S10, the Koa service middleware returns the PDF file stream to the wrong-question book page module to complete the export of the PDF file.

2. The method for exporting a PDF file based on Puppeteer according to claim 1, wherein The question template type corresponds to the layout style.

3. A method for exporting a PDF file based on Puppeteer according to claim 1, characterized in that The print style parameters include header, footer, and watermark parameters.

4. A method for exporting a PDF file based on Puppeteer according to claim 1, wherein, The PDF export service module is a PDF export service module based on puppeteer.

5. A method for exporting a PDF file based on Puppeteer according to claim 1, characterized in that, Step S9 is specifically: The PDF export service module processes the test question data page according to the print style parameters to generate an HTML page, and then exports the generated HTML page as a PDF file stream through the puppeteer rendering engine.

6. The method for exporting a PDF file based on Puppeteer according to claim 5, characterized in that, The puppeteer rendering engine runs in headless browser mode, simulating the environment of a real browser, applying the test question data page to a web page template, and processing the test question data page in the web page template based on the printing style parameters to obtain an HTML page that is exactly the same as the page displayed by the user side in a desktop browser.

Citation Information

Patent Citations

  • Browser-based silent printing client

    CN114527946A

  • Webpage forensics method, apparatus and device

    WO2022126711A1