Method and system for printing content in APP through headless browser

By using a headless browser and wireless connection module in the APP, directly printing content on mobile devices, solving the time-consuming and labor-intensive problem of traditional mobile printing methods, and achieving an efficient and stable mobile printing solution.

CN119902726APending Publication Date: 2025-04-29成都旺小宝科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411981410.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-31
Publication Date
2025-04-29

AI Technical Summary

Technical Problem

Traditional mobile printing methods are time-consuming and labor-intensive, resulting in delays in information transmission and affecting the timeliness and efficiency of customer service.

Method used

Direct printing content is realized in the APP through a headless browser, a wireless connection module is used to detect the printing equipment and establish a communication channel, and a printing control module is used to receive user instructions and send printing tasks.

Benefits of technology

An efficient and stable mobile printing solution is realized, which greatly improves the processing capability of printing tasks and the accuracy of format conversion, and ensures the stability of the system under high load conditions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119902726A_ABST
    Figure CN119902726A_ABST
Patent Text Reader

Abstract

The invention discloses a method and system for printing content in an APP through a headless browser, and belongs to the technical field of mobile office, and the system comprises a headless browser module which is used for rendering the content in the APP in the background and converting the content into data conforming to a printing format; the wireless connection module is used for detecting the printing equipment and establishing a stable communication channel; and the printing control module is used for receiving a printing instruction of a user, selecting a printer and sending a printing task. According to the system and the method, an efficient and stable mobile printing solution is realized, the processing capacity of a printing task and the accuracy of format conversion are greatly improved, the stability of the system under a high-load condition is ensured while server resources are released, and a reliable technical guarantee is provided for mobile office and on-site printing requirements.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of mobile office technology, and more specifically, to a method and system for printing content in an APP through a headless browser. Background Art

[0002] In the modern real estate industry, it is crucial to share information with customers and partners quickly and efficiently. Real estate practitioners often need to print out part of the content in the mobile application (APP) for customers or third-party staff to review and use. The traditional printing method usually involves transferring the content to a computer and then printing it through a computer connected to a printer. This process is not only time-consuming and labor-intensive, but may also cause delays in information transmission, affecting the timeliness and efficiency of customer service.

[0003] With the increasing demand for mobile office, practitioners are increasingly in need of a more direct and convenient way to print the content in the APP. Especially when working on-site or visiting customers, using a mobile device to connect to a printer for quick printing will greatly improve work efficiency. Summary of the invention

[0004] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a method and system for printing content in an APP through a headless browser, thereby realizing an efficient and stable mobile printing solution, greatly improving the processing capability of printing tasks and the accuracy of format conversion, while releasing server resources, ensuring the stability of the system under high load conditions, and providing reliable technical support for mobile office and on-site printing needs.

[0005] The object of the present invention is achieved through the following solutions:

[0006] A system for printing content in an APP through a headless browser, comprising:

[0007] A headless browser module that renders the content in the app in the background and converts it into data in a printable format;

[0008] Wireless connection module, used to detect printing equipment and establish a stable communication channel;

[0009] The printing control module is used to receive the user's printing instructions, select the printer, and send the printing task.

[0010] Furthermore, it also includes an encrypted transmission and authority verification module for ensuring that only authorized devices can perform printing operations to protect sensitive information from being leaked.

[0011] Furthermore, in the headless browser module, the following steps are specifically performed:

[0012] Step 1: Create a resource pool for puppeteer instance management;

[0013] Step 2, create and obtain instances through the resource pool, and after obtaining the puppeteer instance, use the instance to create a page;

[0014] Step 3: When the instance is ready, the page is rendered by calling the setContent method according to the HTML string returned by the backend. After the instance is used, it is returned to the resource pool.

[0015] Step 4: After the page is rendered, convert it into PDF or image format by calling the PDF method or the screenshot method;

[0016] Step 5: Return the converted PDF or image format to the APP in the form of a streaming file to centrally process the printing-related logic.

[0017] A method for printing content in an APP through a headless browser, based on the system as described above, further comprising the following steps:

[0018] Step 1: The employee sends the content he wants to print to the backend through the interface. The content of the interface is specifically the content of the print template configured in the backend. The print template content is transmitted to the backend in the HTML string format;

[0019] Step 2: The backend service retrieves the template content from the database. Each template content has a fixed replacement field. The replacement field is used for identification to facilitate subsequent data filling into the table, and then assembled into a new HTML string;

[0020] Step 3, converting the new HTML string content into a format accepted by the printer and generating a stream file;

[0021] Step 4: pass the stream file to the APP. After the APP caches the stream file locally, it calls the printing capability of the native system.

[0022] Furthermore, in step 3, the new HTML string content is converted into a format accepted by the printer, which specifically includes the following sub-steps:

[0023] The backend passes the HTML string assembled in step 2 to the headless browser module through an ajax request;

[0024] After receiving the string, the headless browser module uses the resource pool management strategy to first create a puppeteer instance, and then update a page instance;

[0025] Then use the page.setContent method to throw the new HTML string generated by the backend into it. The headless browser will generate a page at this time, and the content of this page is the filled table;

[0026] Then use the page screenshot function or PDF conversion function to generate a stream file.

[0027] Furthermore, the resource pool management strategy specifically includes the following sub-steps:

[0028] Step 1: Initialize the default configuration of the resource pool, including the number of instances, survival time, and the maximum number of accesses to each instance. When initializing an instance, its usage count is initialized.

[0029] Step 2: The verification logic automatically destroys the instance when the maximum number of uses is reached, and recreates it to release memory;

[0030] Step 3: After each instance is used, it is returned to the resource pool, and the resource pool releases all methods and actions of the instance.

[0031] The beneficial effects of the present invention include:

[0032] (1) The present invention utilizes headless browser technology, so that the APP can directly call the printer to print content in the same Wi-Fi network environment as the printer. The use of headless browsers not only reduces compatibility issues between devices, but also ensures the quality and accuracy of printouts while improving data transmission efficiency. This innovation will greatly improve the daily workflow of real estate practitioners, improve their work efficiency and enhance customer experience. By achieving direct communication between mobile devices and printers, the present invention is not only suitable for the real estate industry, but can also be widely used in other fields that require mobile office and on-site services, providing new possibilities for digital office in more industries. Specifically, a headless browser can be integrated into the Node.js microservice to achieve automatic conversion of HTML content to the target print format. This integration breaks the traditional browser dependence and provides web page rendering and processing capabilities without a visible interface but with complete functions.

[0033] (2) The present invention improves the resource pool management mechanism, adopts the resource pool to pre-manage headless browser instances, and optimizes system resource utilization. By creating and maintaining a resource pool consisting of multiple browser instances instead of creating instances in real time each time, the system's response speed and resource utilization efficiency are significantly improved.

[0034] (3) The present invention innovates the architecture. In the use of microservice architecture, the microservice architecture built with Node.js focuses on the processing of printing logic, and manages high-concurrency service requests in a modular and scalable manner, thereby improving the stability and maintainability of the overall system.

[0035] (4) The present invention has adaptive format conversion capabilities and has designed and developed an efficient conversion mechanism from HTML to multiple print formats (such as PDF, PNG, etc.) through a headless browser. The output format is dynamically adjusted according to business needs to ensure print quality and device compatibility. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art 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 labor.

[0037] Figure 1 The figure is a flowchart of the steps of the method according to the embodiment of the present invention. DETAILED DESCRIPTION

[0038] All features disclosed in all embodiments in this specification, or steps in all methods or processes implicitly disclosed, except for mutually exclusive features and / or steps, can be combined and / or expanded or replaced in any manner.

[0039] Explanation of terms

[0040] setContent: page object's method for setting page content;

[0041] screenshot: screenshot function method of page object;

[0042] page.setContent: The page object's method for setting page content.

[0043] In view of the current situation in the background, the inventors of this application have experienced creative thinking and believe that: in the current context of mobile printing technology, there are two major technical challenges that need to be solved: (1) HTML format conversion problem: the existing solution has the problem of being unable to directly convert HTML content into the printing format required by the business end. Different printers or business needs require specific formats (such as PDF, PNG, etc.), and the direct conversion process is complicated and prone to errors. The lack of efficient automated tools to handle such conversions may result in substandard printing quality and even cause compatibility issues. (2) Handling of high-concurrency printing needs: as demand increases, how to efficiently handle a large number of concurrent printing requests has become a major problem. In high-concurrency situations, the frequent startup and destruction of headless browsers may cause server resource constraints, occupy a large amount of CPU and memory, and then cause the system to freeze or crash. Therefore, a mechanism that can effectively manage resources is needed to cope with sudden high-concurrency requests.

[0044] In view of the business needs and existing technical problems discovered above, the present invention conceives a set of solutions. In an embodiment, a method and system for printing content in an APP through a headless browser are specifically proposed.

[0045] The concept of the present invention specifically includes the following technical improvements:

[0046] (1) Introduction of microservice architecture: Create a dedicated microservice based on Node.js. The specific steps include:

[0047] Step 1. First, establish a resource pool service. This resource pool is mainly used to manage puppeteer instance management, because the startup and destruction of each instance consumes a lot of memory. In order to prevent this and prevent high concurrency from causing downtime.

[0048] Step 2: Create and obtain instances through the resource pool. After obtaining the puppeteer instance, use the instance to create a page.

[0049] Step 3: When the instance is ready, take the HTML string returned by the backend and render the page by calling the setContent method.

[0050] Step 4: After the page is rendered, you can call the pdf method or screenshot method to convert it into pdf or image.

[0051] Step 5: Return the converted resources to the APP end in the form of streams to centrally process the logic related to printing. Modular management is achieved through the microservice architecture, making the system more scalable and maintainable, effectively separating the business processing related to printing, and ensuring that other system functions run independently.

[0052] (2) Use of headless browser containers: When the Node.js service is started, a set of headless browser instance containers are created and maintained in advance. Through these containers, the HTML content that the customer wants to print is read invisibly. The headless browser can run in the "background" to simulate the real browser environment so that content rendering and processing can be carried out smoothly.

[0053] (3) Adaptive format conversion: Use a headless browser to convert HTML content into the specific format required by the business end. Adaptively adjust the output format according to different needs to ensure that the converted content can directly meet business needs and the compatibility of printing equipment, and achieve high-quality, customized print output.

[0054] (4) Efficient management of resource pool: To solve the high concurrency problem, a resource pool is created when the Node.js service is started, and 10-20 headless browser instances are configured in advance. Whenever there is a print request, a ready-made headless browser instance is obtained from the resource pool for processing, and then returned to the resource pool after use. By recycling the browser instance, the overhead of instance creation and destruction is reduced, the waste of resources caused by repeated browser startup is avoided, and the system response speed and stability are improved. This mechanism can quickly adapt to high concurrency requirements and maintain smooth service operation. In the specific implementation, the present invention designs a unique headless browser resource pool management strategy, especially the instance creation, allocation and release mechanism. In view of the fact that the initialization of the puppeteer instance requires a large amount of IO, resulting in an abnormal increase in memory, severe high concurrency, and the server being paralyzed, the present invention is conceived that the main task of the resource management strategy is to solve a large number of IO operations, so a lot of puppeteer instances can be prepared in advance, and the memory can be released immediately after use to prevent the problem of memory overflow, which may include the following sub-steps:

[0055] Step 1: Initialize the default configuration of the resource pool, including the number of instances, survival time, and the maximum number of accesses to each instance;

[0056] Step 2: Initialize the number of times the instance is used when it is initialized;

[0057] Step 3: Verify the logic and automatically destroy the instance when the number of times used is less than the maximum, and recreate it to release memory;

[0058] Step 4: After each instance is used, it is automatically returned to the resource pool, and the resource pool releases all methods and actions of the instance to ensure efficient and stable operation in a high-concurrency environment.

[0059] The technical solution of the present invention realizes an efficient and stable mobile printing solution through Node.js microservices and resource pool management, greatly improves the processing capability of printing tasks and the accuracy of format conversion, while releasing server resources, ensuring the stability of the system under high load conditions, and providing reliable technical support for mobile office and on-site printing needs.

[0060] Example 1

[0061] In the overall technical concept of the present invention, a technical solution is specifically proposed to realize content printing in APP by using headless browser technology. The core of this technical solution is to realize seamless connection with the printer in the local area network through the built-in headless browser module of APP, so that users can conveniently call the printer directly from the mobile device to print in the same Wi-Fi environment. Specifically, in the preferred embodiment of the system of the present invention, the following key modules are included:

[0062] A headless browser module is used to render the content in the APP in the background and convert it into data in a format suitable for printing; this module is specifically implemented based on Puppeteer technology. Puppeteer is a Node library that provides a high-level API to control the Chromium or Chrome browser through the DevTools protocol. Among them, the DevTools protocol is a protocol that allows developer tools to communicate with browsers. It provides rich functions, allowing developers to remotely control the Chrome browser, including operating the DOM, monitoring network requests, debugging code, taking screenshots, etc. This embodiment uses the downloaded chromed plug-in to drive Chrome through code calls. By default, it is in headless mode. More specifically, in this embodiment, if Figure 1 As shown in the figure, create a dedicated microservice based on Node.js, which includes the following sub-steps:

[0063] Step 1. Establish a resource pool service. This resource pool is mainly used to manage puppeteer instance management. Because the startup and destruction of each instance consumes a lot of memory, in order to prevent this and prevent high concurrency from causing downtime.

[0064] Step 2: Create and obtain instances through the resource pool. After obtaining the puppeteer instance, use the instance to create a page.

[0065] Step 3: When the instance is ready, take the HTML string returned by the backend and render the page by calling the setContent method.

[0066] Step 4: After the page is rendered, you can call the pdf method or screenshot method to convert it into pdf or image.

[0067] Step 5: Return the converted resources to the APP end in the form of streams to centrally process the logic related to printing. Modular management is achieved through the microservice architecture, making the system more scalable and maintainable, effectively separating the business processing related to printing, and ensuring that other system functions run independently.

[0068] Wireless connection module, used to detect available printing devices in the surrounding area and establish a stable communication channel;

[0069] The print control module is used to receive the user's print instruction, select a suitable printer, and send the print task.

[0070] Through the above integration method, users can achieve fast and accurate printing without complicated connection or conversion operations between mobile devices and computers, which significantly simplifies the printing process, improves the user's operational flexibility while on the move, and reduces the risk of errors in the information transmission process.

[0071] In addition, to ensure the security and privacy of the printed content, the present invention also introduces an encrypted transmission and permission verification mechanism to ensure that only authorized devices can perform printing operations and protect sensitive information from being leaked. This innovative design provides an efficient and secure printing solution for real estate practitioners and other users who need on-site printing services.

[0072] Example 2

[0073] In a preferred embodiment of the method of the present invention, based on the above system, the following process is performed:

[0074] Step 1: The practitioner sends the content he wants to print to the backend through the interface. At this time, the content of the interface is mainly some printing template content configured in the background. These contents are mainly in the form of HTML strings and then transmitted to the backend through ajax requests.

[0075] Step 2: The backend service will retrieve the template content from the database. Fixed replacement fields are written in each template content. These replacement fields are used for identification, so that subsequent data can be perfectly filled into the table, and then a new HTML string is formed.

[0076] Step 3. Convert the HTML content into a format accepted by any printer, such as images and pnf. The specific method is that the backend transmits the assembled HTML string to a dedicated microservice based on Node.js (Node service for short) through an ajax request. After receiving the string, first create a puppeteer instance, then update a new page instance, and then use the page.setContent method to throw the new HTML string generated by the backend into it. The headless browser will generate a page with the filled-in table as the content of the page. Then use the screenshot function of the page or the PDF conversion function to generate a streaming file.

[0077] Step 4: The Node service transmits the stream file to the APP. After the APP caches the stream file locally, it calls the printing capability of the native system.

[0078] In traditional cases, staff print a lot of forms or contracts, and need to fill in a lot of basic information about the house, price, buyer and seller. The workload is huge and it is easy to make mistakes and the content is inconsistent with the system, which increases the workload of staff. The embodiment of the present invention allows staff to check whether the key content is consistent without having to write the content by hand.

[0079] Traditional printing on the job site requires the computer to be connected to a printer for printing, but it is impossible for the staff to hold the computer and print at any time. The embodiment of the present invention solves the problem that the staff can print at any location in the office as long as the WIFI function is connected.

[0080] The units involved in the embodiments of the present invention may be implemented by software or hardware, and the units described may also be arranged in a processor. The names of these units do not, in some cases, limit the units themselves.

[0081] According to one aspect of an embodiment of the present invention, a computer program product or a computer program is provided, the computer program product or the computer program includes a computer instruction, and the computer instruction is stored in a computer-readable storage medium. A processor of a computer device reads the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the computer device executes the method provided in the above various optional implementations.

[0082] As another aspect, an embodiment of the present invention further provides a computer-readable medium, which may be included in the electronic device described in the above embodiment; or may exist independently without being assembled into the electronic device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by an electronic device, the electronic device implements the method described in the above embodiment.

Claims

1. A system for printing content in an APP through a headless browser, characterized in that: include: A headless browser module that renders the content in the app in the background and converts it into data in a printable format; Wireless connection module, used to detect printing equipment and establish a stable communication channel; The printing control module is used to receive the user's printing instructions, select the printer, and send the printing task.

2. The system for printing content in an APP through a headless browser according to claim 1, characterized in that: It also includes encrypted transmission and permission verification modules to ensure that only authorized devices can perform printing operations to protect sensitive information from being leaked.

3. The system for printing content in an APP through a headless browser according to claim 1, characterized in that: In the headless browser module, the following steps are specifically performed: Step 1: Create a resource pool for puppeteer instance management; Step 2, create and obtain instances through the resource pool, and after obtaining the puppeteer instance, use the instance to create a page; Step 3: When the instance is ready, the page is rendered by calling the setContent method according to the HTML string returned by the backend. After the instance is used, it is returned to the resource pool. Step 4: After the page is rendered, convert it into PDF or image format by calling the PDF method or the screenshot method; Step 5: Return the converted PDF or image format to the APP in the form of a streaming file to centrally process the printing-related logic.

4. A method for printing content in an APP through a headless browser, characterized in that: The system according to claim 1 further comprises the following steps: Step 1: The employee sends the content he wants to print to the backend through the interface. The content of the interface is specifically the content of the print template configured in the backend. The print template content is transmitted to the backend in the HTML string format; Step 2: The backend service retrieves the template content from the database. Each template content has a fixed replacement field. The replacement field is used for identification to facilitate subsequent data filling into the table, and then assembled into a new HTML string; Step 3, converting the new HTML string content into a format accepted by the printer and generating a stream file; Step 4: pass the stream file to the APP. After the APP caches the stream file locally, it calls the printing capability of the native system.

5. The method for printing content in an APP through a headless browser according to claim 4, characterized in that: In step 3, the new HTML string content is converted into a format accepted by the printer, which specifically includes the following sub-steps: The backend passes the HTML string assembled in step 2 to the headless browser module through an ajax request; After receiving the string, the headless browser module uses the resource pool management strategy to first create a puppeteer instance, and then update a page instance; Then use the page.setContent method to throw the new HTML string generated by the backend into it. The headless browser will generate a page at this time, and the content of this page is the filled table; Then use the page screenshot function or PDF conversion function to generate a stream file.

6. The method for printing content in an APP through a headless browser according to claim 1, characterized in that: The resource pool management strategy specifically includes the following sub-steps: Step 1: Initialize the default configuration of the resource pool, including the number of instances, survival time, and the maximum number of accesses to each instance. When initializing an instance, its usage count is initialized. Step 2: The verification logic automatically destroys the instance when the maximum number of uses is reached, and recreates it to release memory; Step 3: After each instance is used, it is returned to the resource pool, and the resource pool releases all methods and actions of the instance.