Method and terminal for high-fidelity conversion of a docx template into a pdf file

By converting docx document templates to HTML document templates and adjusting their formatting, the problem of formatting distortion when converting docx templates to PDF files is solved, achieving high-fidelity conversion, improving the efficiency and flexibility of official document template production, and reducing costs.

CN116010496BActive Publication Date: 2026-04-24FUJIAN RONGJI SOFTWARE +1
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
FUJIAN RONGJI SOFTWARE
Filing Date
2022-12-22
Publication Date
2026-04-24

AI Technical Summary

Technical Problem

Existing technologies often result in format distortion when converting docx templates to pdf files, leading to printing results that do not meet strict printing requirements. Furthermore, they are costly to develop and test, and inefficient.

Method used

By obtaining the document template file, setting placeholder business data and defining format expressions, the docx document template is converted into an HTML document template. The expressions are then corrected, business data is filled in, and the format is adjusted, ultimately converting it into a PDF file.

Benefits of technology

It achieves low-cost, high-efficiency, and high-fidelity conversion, enhances the flexibility and adaptability of official document templates, reduces production difficulty and replacement costs, and improves the fidelity and reusability of electronic official documents.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116010496B_ABST
    Figure CN116010496B_ABST
Patent Text Reader

Abstract

The application discloses a kind of high fidelity conversion docx template to pdf file method and terminal, obtain document template file, the expression of including placeholder business data and the expression of definition format in the document template file;Docx document template is converted into HTML document template;The expression that is broken in HTML document template file is revised;Business data is filled into HTML document template, and format is adjusted and revised;HTML file is converted into PDF file.Low cost, high efficiency, high fidelity is realized to the conversion of official document docx template, and electronic official document pdf file is effectively helped to improve the flexibility, adaptability and production efficiency of official document template production, reduces the difficulty and replacement cost of electronic official document production, improves the high fidelity degree and reusable degree of electronic official document.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of document processing technology, and in particular to a method and terminal for high-fidelity conversion of docx templates into pdf files. Background Technology

[0002] With the increasing development and popularization of information technology, daily life and work are becoming more and more reliant on computers. Moreover, under the global concept of carbon neutrality, paperless office work is constantly developing and expanding. A large number of offline official documents and certificates are undergoing electronic reforms, and document template technology has become an indispensable technology.

[0003] By creating templates for official documents and certificates in a specific format, and then replacing and rendering them according to specific business data, corresponding document and certificate files are dynamically generated. Mainstream technologies for implementing electronic official documents and certificates include:

[0004] Built-in hard coding: First, the layout of the corresponding template is coded directly using the API of the PDF component within the code. Then, the business data processing logic is written. Finally, the business data values ​​are set into the PDF file to generate official document documents.

[0005] Specific production program: First, a specific production program is developed. Then, the configuration is designed and saved as a specific format document / certificate template configuration file according to the style of official documents and certificates. Finally, the specific production program reads the document / certificate configuration file and document / certificate business data to generate document / certificate PDF files.

[0006] Print directly from the HTML interface: Use the browser's print function, right-click and select print to output a PDF file. Alternatively, use the browser's built-in print function to set the print area first, then print the PDF.

[0007] Of the methods mentioned above, built-in hard coding, specific production programs, and direct printing from the HTML interface all require a significant amount of manual development and testing, resulting in high costs and slow efficiency.

[0008] To address this, existing technology provides a method for converting docx templates to XML / HTML and then to PDF files. First, the official document template is edited in docx format and converted to XML / HTML. Then, FreeMarker or other tools are used to populate the business data for the official document. Finally, it is converted into an official document PDF file for printing.

[0009] However, converting a docx template to XML / HTML and then to PDF results in significant format distortion due to the two conversions, often leading to print quality that does not meet strict printing requirements. Summary of the Invention

[0010] The technical problem to be solved by the present invention is to provide a method and terminal for converting docx templates into PDF files with high fidelity, which can convert document template files into PDF files with high fidelity.

[0011] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:

[0012] A method for high-fidelity conversion of docx templates to pdf files includes the following steps:

[0013] S1. Obtain a document template file, which includes expressions for placeholder business data and expressions defining the format;

[0014] S2. Convert the docx document template into an HTML document template;

[0015] S3. Correct expressions that were mistakenly broken in the HTML document template file;

[0016] S4. Populate the business data into the HTML document template and adjust and correct the format;

[0017] S5. Convert HTML files to PDF files.

[0018] To solve the above-mentioned technical problems, another technical solution adopted by the present invention is as follows:

[0019] A terminal for high-fidelity conversion of docx templates to pdf files includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the method described above.

[0020] The beneficial effects of this invention are as follows: A method for high-fidelity conversion of docx templates to PDF files, by setting expressions that define placeholder business data and expressions that define format, first converts the docx document template into an HTML document template, then fills the expressions in the HTML document template with business data and adjusts and corrects the format, and finally converts the HTML file into a PDF file. This achieves low-cost, high-efficiency, and high-fidelity conversion of official document docx templates into electronic official document PDF files, effectively helping users improve the flexibility, adaptability, and efficiency of official document template creation, reducing the difficulty and replacement cost of creating electronic official documents, and improving the high fidelity and reusability of electronic official documents. Attached Figure Description

[0021] Figure 1 This is a flowchart illustrating a method for high-fidelity conversion of a docx template to a pdf file according to an embodiment of the present invention;

[0022] Figure 2 This is a schematic diagram of a blank certificate template file involved in an embodiment of the present invention;

[0023] Figure 3 This is a schematic diagram of a document template file with expressions filled in, which is an embodiment of the present invention.

[0024] Figure 4 This is a schematic diagram of the structure of a terminal for high-fidelity conversion of docx templates into pdf files according to an embodiment of the present invention.

[0025] Label Explanation:

[0026] 1. A terminal; 2. A processor; 3. A memory. Detailed Implementation

[0027] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.

[0028] Please refer to Figures 1 to 3 A method for high-fidelity conversion of docx templates to pdf files, including the following steps:

[0029] S1. Obtain a document template file, which includes expressions for placeholder business data and expressions defining the format;

[0030] S2. Convert the docx document template into an HTML document template;

[0031] S3. Correct expressions that were mistakenly broken in the HTML document template file;

[0032] S4. Populate the business data into the HTML document template and adjust and correct the format;

[0033] S5. Convert HTML files to PDF files.

[0034] As described above, the beneficial effects of this invention are as follows: A method for high-fidelity conversion of docx templates to PDF files, by setting expressions that define placeholder business data and expressions that define format, first converts the docx document template into an HTML document template, then fills the expressions in the HTML document template with business data and adjusts and corrects the format, and finally converts the HTML file into a PDF file. This achieves low-cost, high-efficiency, and high-fidelity conversion of official document docx templates into electronic official document PDF files, effectively helping users improve the flexibility, adaptability, and efficiency of official document template creation, reducing the difficulty and replacement cost of creating electronic official documents, and improving the high fidelity and reusability of electronic official documents.

[0035] Furthermore, the format adjustment specifically includes table correction, style processing, font correction, image filling, and appendix processing.

[0036] As described above, adjustments have been made to address the format differences between HTML files and document template files.

[0037] Furthermore, the table correction specifically includes:

[0038] The table rows are generated based on the number of rows of data. If the table rows span multiple pages, a table header is generated on each page.

[0039] Fill in the actual values ​​for each line of expressions;

[0040] Based on the table data, adjust the width and height of the table cells.

[0041] As described above, although the two-rendering approach seems cumbersome, it separates the operations of style and data, allowing for more accurate style processing by referring to the data in the previous and next rows or the entire table.

[0042] Furthermore, the style processing specifically involves converting the paragraph and page formats of the document template file into an HTML structure and then modifying them.

[0043] As described above, the conversion from docx document template files to HTML file format has been achieved.

[0044] Furthermore, the page format includes page width, header, footer, and bottom area.

[0045] As described above, it not only achieves the conversion of the usual page width, header and footer, but also supports the bottom area of ​​the document template file required for the signature after printing.

[0046] Furthermore, the expression includes one or more of the following:

[0047] The bottom alignment expression is placed on the line before the content that needs to be displayed at the bottom to control the bottom alignment of the content.

[0048] The appendix display expression controls how the page containing the expression is displayed as an appendix.

[0049] A table line break expression that forces excessively long foreign language words to wrap to a new line when the table cell width is fixed.

[0050] As described above, the expression ensures that the format before and after conversion meets the requirements.

[0051] Furthermore, step S3 specifically includes:

[0052] If the number of tags inside curly braces is the same, but the tags starting from left to right are different, then delete the tags "", "", and "", from left to right. "and" "Up to "}";

[0053] If the number of tags inside curly braces is the same, but the tags start from left to right, then... If the expression is enclosed by a parent "span", then it is determined whether the expression is enclosed by a parent "span". If it is, the "span" tag in the expression is deleted. If not, the inner tag is moved to both sides of the expression to wrap the expression.

[0054] If the parentheses are inside the start tag If there are many, search from left to right, extract the "span" start tags that cannot be matched, delete the matching "span" start and end tags, and insert the extracted tags to the right of "}".

[0055] If the parentheses are used, the ending tag should be... If there are many, extract the unmatched "span" closing tags, delete the matching "span" start and end tags, and insert the extracted tags to the right of "}".

[0056] As described above, the repair of expressions has been achieved. For complex expressions, it is only necessary to repair each field in accordance with the above method.

[0057] A terminal for high-fidelity conversion of docx templates to pdf files includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the method described above.

[0058] This invention is used for the low-cost, high-efficiency, flexible, adaptable, and high-fidelity design, configuration, generation, modification, and conversion of official document docx templates into pdf files.

[0059] Please refer to Figures 1 to 3 Embodiment 1 of the present invention is as follows:

[0060] Using built-in hard coding requires developing, coding, and testing for each document and certificate with different styles, which requires a large investment of development manpower. Furthermore, once the style changes, personnel must be re-invested in development, coding, and testing, resulting in a huge waste of human resources.

[0061] Using a specific production process requires operators to spend a lot of time learning the unique operations of that process, designing, configuring templates, and verifying and testing various official documents and certificates. This raises the entry barrier for operators, and if an error occurs, it requires cooperation from multiple parties, resulting in a significant waste of human resources.

[0062] Moreover, the official document template format configured in this way is its inherent format. Once the technology changes (such as .NET -> Java), a large number of developers will need to redevelop the specific production program, resulting in huge costs.

[0063] Using browser functionality to directly convert HTM to PDF requires developers to create corresponding HTM document templates. These templates are complex and detailed, and can only be processed by developers, resulting in extremely high processing costs. Furthermore, the logic for processing business data is extensive and inefficient.

[0064] While editing official document templates in docx format is the most convenient for creators and allows for the most accurate data entry, the format has been converted twice. docx supports functions that are not entirely consistent with HTML, and HTML and pdf also support functions that are not entirely consistent, resulting in many formatting distortions. Consequently, the printed results often do not meet the strict printing requirements for official documents.

[0065] Therefore, this invention proposes a method for high-fidelity conversion of docx templates to pdf files, comprising the following steps:

[0066] S1. Obtain a document template file, which includes expressions for placeholder business data and expressions defining the format.

[0067] The document template file is editable like a regular docx file, but dynamic data in the document template file is placed using the expression "${propname}". In this embodiment, the editor for editing the document template file is Yongzhong Editor, which supports domestically produced devices.

[0068] S2. Use the editor's built-in functions to convert the docx document template into an HTML document template.

[0069] S3. Correct expressions that were mistakenly broken in the document template file.

[0070] To correct expressions that might be accidentally broken by the editor, please refer to [link / reference]. Figure 2-3 Document template files inevitably contain dynamic data that needs to be replaced with business data. Therefore, placeholders must be provided to indicate the replacement location of the business data, for example, the shipper's expression ${CONSIGNORCNAME}${n}${CONSIGNORCNAME}.

[0071] When generating a PDF, ${CONSIGNORCNAME} will be replaced with the actual value of CONSIGNORCNAME (the sender's Chinese name) in the environment variable, and ${n} will be replaced with a newline character.

[0072] In addition to the business data expressions mentioned above, this device also provides style expressions for special effects, such as: ${BOTTOM} can be placed one line before the content that needs to be displayed at the bottom to achieve automatic bottom alignment; ${ATTACHMENT, expression} controls whether the current page is displayed (if there is no content, the appendix will not be displayed); ${SHHWARP} forces line breaks for excessively long foreign words when the table cell width is fixed, etc.

[0073] However, because editors (MSWORD, WPS, Yongzhong, OFFICE, etc.) often split the expression ${CONSIGNORCNAME} into multiple fragments, the converted HTML may be... ${ CONSIGNORCNAME } When encountering complex expressions, the possibility of being incorrectly cut off is greater, and the situation becomes more complicated.

[0074] Step S2 will make corrections for the various cases of being cut off. After correction, the expressions will not only look together, but will also not be cut off in the actual HTML.

[0075] During template creation, it's inevitable that you'll encounter situations like copying text from non-txt files, adjusting fonts, or accidentally adding line breaks (the perfect approach is to write the expression in a text file (Notepad), then copy it into the template without making any adjustments). These operations affect the expression, which will appear in the HTML as follows:

[0076] 1. Within curly braces, the number of tags is the same, but the tags start from left to right.

[0077] <span style=”…”> ${xxx <span style=”…”> dd }

[0078] 2. Within curly braces, the number of tags is the same, but the tags start from left to right.

[0079] <span style=”…”> ${xxx <span style=”…”> dd }

[0080] 3. The starting label is enclosed in curly braces. many

[0081] ${xxx <span style=”…”> <span style=”…”>d d}

[0082] 4. End tag within curly braces many

[0083] ${xxx dd }

[0084] 5. The starting label is enclosed in curly braces. many

[0085] ${if,true or false, Value 1 , Value 2}

[0086] Since we are dealing with HTML tags, we can move the "incorrect tags" according to the inheritance properties of CSS to ensure the style of the text in its own scope while minimizing interference with other tags.

[0087] For operation 1: Using the "${" symbol as the tag base, we determine if the tag to the left of the base is a "span" starting tag, and then determine if the expression is a non-logical simple expression. Therefore, we delete from left to right. "、" ”" "Three blocks up to "}". This results in the final effect of the expression range. <span style=”…”> ${xxxdd} "Continuous, paired with the "span" tag, the style effect covers the entire expression."

[0088] Operation 2: Using the "${" symbol as the tag base, determine if the left side of the base point is a "span" closing symbol. Then determine if the expression is a non-logical simple expression. Next, check if the expression is enclosed by a parent "span": If enclosed, delete the "span" tag within the expression; if not, move the inner tag to both sides of the expression to wrap it and prevent style loss. Final result: <span style=”…”> <span style=”…”> ${xxxdd}

[0089] Perform the following operation on line 3: Using the "${" symbol as the tag base, determine if the left side of the base is a "span" start character, and then determine if the expression is a non-logical simple expression. Search from left to right, extract unmatched "span" start tags, delete matched "span" start and end tags, and insert the extracted tags to the right of "}" to maintain the overall tag configuration while preserving the style. Final result: ${xxxdd} <span style=”…”>

[0090] Perform the following operation on step 4: Same as step 3, but this time insert the extra "span" closing tag to the right of "}". Final result: ${xxxdd}

[0091] The operation on 5 is as follows: Using the "${" symbol as the tag base, it is determined that the left side of the base point is a "span" start character, and then the expression is determined to be a complex logical if expression. Therefore, according to the definition of the if field, the content " Value 1 "or" Value 2 will be displayed as text connected to the right of the base point. Therefore, to balance the start and end tags of the "span", the tags of the value 1 field are first paired according to the processing logic of operations 1 to 4, and any extra tags are inserted at the beginning of the value 2 field. Then, the tags of the value 2 field are paired, and any extra tags are inserted to the right of the "}". Finally, the tags of the entire field are paired, resulting in the following final effect: ${if,true or false, Value 1 , Value 2}

[0092] S4. Populate the business data into the HTML document template and adjust and correct the format.

[0093] In this embodiment, the third-party tool FreeMarker is used to populate data into HTML. Due to the complexity of the format, the population process needs to be done in steps. For example, the table is first filled with the complete table format according to the number of rows, and then the data is populated.

[0094] Specifically, the formatting adjustments include table correction, style processing, font correction, image filling, and appendix processing.

[0095] The table correction specifically involves generating table rows based on the number of rows in the data. If a table row spans multiple pages, a table header is generated on each page.

[0096] Fill in the actual values ​​for each line of expressions;

[0097] Based on the table data, adjust the width and height of the table cells.

[0098] Specifically:

[0099] First, generate table rows based on the number of rows in the data. If the table spans multiple pages, you also need to consider adding table headers on a new page.

[0100] Then, extract the actual value from the expression in the line.

[0101] Adjust cell width / height. Obtain each table containing the `mso-tablelayout-alt:fixed` attribute, then generate a `col` value based on the column width given in the last row and set it within the table. For cells with set heights, due to differences between docx and HTML specifications, a DIV (or other block element) with a set height must be manually added. Then, nest a table of individual cells within this nested element. These nested cells must manually inherit the style attributes of the source cell to correctly represent the fixed cell height and retain the original style information.

[0102] Although the two-rendering approach seems cumbersome, it separates the style and data operations, allowing for more accurate style processing by referencing data from the previous and next rows or the entire table.

[0103] In this embodiment, the style processing specifically involves converting the paragraph and page formats of the document template file into an HTML structure and then modifying them.

[0104] The page formatting includes page width, header, and footer concepts unique to docx files. For document template files that require printing, it also includes a "bottom area" marker (used for signatures, seals, etc.), which also needs to be converted and displayed accordingly. Because when the template is created, the final number of pages and lines in the PDF is unknown, so it is impossible to control the signature and seal areas to fall on the last page through style. Therefore, this invention adds a ${bottom} expression to support this function.

[0105] In this embodiment, the font correction includes replacing unsupported fonts with similar fonts and performing the replacement by looking up a table according to different units.

[0106] Depending on the differences in font support between HTML and DOCX, multiple fonts in the stylesheet need to be merged and their order preset. For fonts not included by default, specific font libraries need to be loaded for the PDF generator; unavailable fonts need to be replaced with similar ones. Additionally, font sizes need to be converted according to different units. Furthermore, special formatting issues, such as the potential loss of multiple consecutive spaces due to differing specifications, also need to be addressed.

[0107] The aforementioned appendix processing specifically involves generating the phrase "see appendix N" based on a field of a specific document, and then adding the appendix content to the PDF. This is commonly seen in photo and image formats.

[0108] S5. Convert HTML files to PDF files.

[0109] Specifically, in this embodiment, the third-party open-source component iText is used to convert the corrected HTML into PDF.

[0110] Please refer to Figure 4 Embodiment two of the present invention is as follows:

[0111] A terminal 1 for high-fidelity conversion of docx templates to pdf files includes a memory 3, a processor 2, and a computer program stored on the memory 3 and executable on the processor 2. When the processor 2 executes the computer program, it implements the steps of the above embodiment 1.

[0112] In summary, the present invention provides a method for high-fidelity conversion of docx templates to PDF files. By setting expressions that define placeholder business data and formatting expressions, the docx document template is first converted into an HTML document template. The expressions in the HTML document template are then populated with business data and the formatting is adjusted and corrected. Finally, the HTML file is converted into a PDF file. This method achieves low-cost, high-efficiency, and high-fidelity conversion of official document docx templates into electronic official document PDF files. It effectively helps users improve the flexibility, adaptability, and efficiency of official document template creation, reduces the difficulty and cost of creating electronic official documents, and enhances the high fidelity and reusability of electronic official documents.

[0113] The above description is merely an example of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention's specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.

Claims

1. A method for high-fidelity conversion of docx templates to pdf files, characterized in that, Including the following steps: S1. Obtain a document template file, which includes expressions for placeholder business data and expressions defining the format; S2. Convert the docx document template into an HTML document template; S3. Correct expressions that were mistakenly broken in the document template file; S4. Populate the business data into the HTML document template and adjust and correct the format; S5. Convert HTML files to PDF files; Step S3 specifically includes: If the number of tags inside curly braces is the same, but the tags starting from left to right are different, then delete the tags "" and "" from left to right. "and" "Up to "}"; If the number of tags inside curly braces is the same, but the tags start from left to right, then... If the expression is enclosed by a parent "span", then it is determined whether the expression is enclosed by a parent "span". If it is, the "span" tag in the expression is deleted. If not, the inner tag is moved to both sides of the expression to wrap the expression. If the parentheses are inside the start tag If there are many, search from left to right, extract the "span" start tags that cannot be matched, delete the matching "span" start and end tags, and insert the extracted tags to the right of "}". If there are multiple closing tags within the curly braces, extract the unmatched "span" closing tags, delete the matching "span" start and end tags, and insert the extracted tags to the right of "}".

2. The method for high-fidelity conversion of docx templates to pdf files according to claim 1, characterized in that, The format adjustment specifically includes table correction, style processing, font correction, image filling, and appendix processing.

3. The method for high-fidelity conversion of docx templates to pdf files according to claim 2, characterized in that, The table correction specifically includes: The table rows are generated based on the number of rows of data. If the table rows span multiple pages, a table header is generated on each page. Fill in the actual values ​​for each line of expressions; Based on the table data, adjust the width and height of the table cells.

4. The method for high-fidelity conversion of docx templates to pdf files according to claim 2, characterized in that, The style processing specifically involves converting the paragraph and page formats of the document template file into an HTML structure and then modifying them.

5. The method for high-fidelity conversion of docx templates to pdf files according to claim 4, characterized in that, The page format includes page width, header, footer, and bottom area.

6. The method for high-fidelity conversion of docx templates to pdf files according to claim 1, characterized in that, The expression includes one or more of the following: The bottom alignment expression is placed on the line before the content that needs to be displayed at the bottom to control the bottom alignment of the content. The appendix display expression controls how the page containing the expression is displayed as an appendix. A table line break expression that forces excessively long foreign language words to wrap to a new line when the table cell width is fixed.

7. A terminal for high-fidelity conversion of docx templates to pdf files, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method according to any one of claims 1-6.

Citation Information

Patent Citations

  • Signed file generation method and device, computer equipment and storage medium

    CN113901777A