Rendering process-based PDF element extraction method suitable for RAG

Through a PDF element extraction method based on the rendering process, the information loss problem of PDF parsing tools in the existing technology when processing complex document structures is solved, meeting the high-quality text input requirements of the RAG system, and improving the system Performance and data processing reliability.

CN119337817APending Publication Date: 2025-01-21LINKER
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411236071.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-04
Publication Date
2025-01-21

AI Technical Summary

Technical Problem

When handling complex document structures, existing PDF parsing tools often ignore image data or lose structural information during the extraction of table data, resulting in insufficient information integrity and accuracy, which cannot meet the high-quality text input requirements of RAG systems.

Method used

Adopt a PDF element extraction method based on the rendering process, decompose the PDF file into a block through the target detection, identify and mark the block category, process the use of seat occupies and the format model model for pictures and forms, and the data extracted from the extraction Convert to Markdown format to keep the location information to ensure the accuracy and integrity of the data.

Benefits of technology

It provides the RAG system with uniformly formatted and easy-to-process text input, which greatly reduces the loss of information, solves the problems of existing PDF parsing tools in processing complex content, and improves system performance and data processing reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119337817A_ABST
    Figure CN119337817A_ABST
Patent Text Reader

Abstract

The invention discloses a PDF (Portable Document Format) element extraction method based on a rendering process and suitable for RAG (Random Access Gateway). According to the method, the PDF content is decomposed into a plurality of blocks, and the blocks are subjected to type judgment and processing. Marking the image data in the form of text placeholders, and retaining the position and reference value of the image in the document; and for the table data, the table data is converted into an HTML format so as to maintain the structural integrity, and the consistency and readability of the table data in a plain text environment are ensured. And finally converting all extracted contents into a Markdown format to be output. According to the method, text input which is uniform in format and easy to process is provided for an RAG system, information loss is greatly reduced, the problem that an existing PDF analysis tool is difficult to process complex content is solved, and system performance and data processing reliability are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer data processing, and in particular to a PDF element extraction method based on a rendering process suitable for RAG. Background Art

[0002] With the popularization of digital information, PDF (Portable Document Format) has become one of the most widely used document formats. However, existing PDF parsing tools often face challenges when processing complex document structures, such as images and tables. Traditional PDF parsing methods often ignore image data or lose structural information during the extraction of table data, resulting in insufficient information integrity and accuracy.

[0003] In the field of information retrieval and natural language processing (NLP), the RAG (Retrieval-Augmented Generation) system improves the ability to answer complex queries by combining retrieval and generation techniques. The RAG system relies on high-quality text input data, but existing PDF parsing tools cannot meet this requirement. In order to improve the accuracy and efficiency of data extraction, a new element extraction method based on PDF rendering is urgently needed. Summary of the invention

[0004] The present invention mainly solves the technical problem that the prior art cannot provide PDF document data that can satisfy the RAG system, and provides a PDF element extraction method based on a rendering process suitable for RAG.

[0005] The present invention aims to solve the above technical problems mainly through the following technical solutions: a PDF element extraction method based on rendering process applicable to RAG, comprising the following steps: S1. Perform object detection on each page of the PDF file and decompose the content into several blocks. Each block is surrounded by a recognition box. Each recognition box is labeled according to the recognized category, which includes text, picture, table and picture description. S2. For standard PDF documents, parse through PDF tools, read the category of each block in the formatted output process, and compare it with the category obtained in step S1. If there is a difference, the PDF formatted output shall prevail; for non-standard PDF documents, enlarge the identification box by 1.05 times; the text in the standard document can be selected and copied, and the copied text is consistent with the text in the PDF; on the contrary, non-standard documents are generally scanned images, which are essentially images and cannot be selected and copied.

[0006] S3. For pictures, first replace the place where the picture in the area exists with a unique placeholder, and save the picture in this area with the unique placeholder as the file name; the traditional processing method cannot handle the picture data and can only ignore it. This solution replaces it with a placeholder, and when the placeholder is read, the corresponding picture file is retrieved, thereby retaining the position and reference value of the picture in the document; S4. For a standard PDF document, draw a table and convert it into an image. Then use the image-to-table model to convert the image data of the table into Excel table data. Then read the table data into a DataFrame and convert it into HTML format. For non-standard PDF documents, directly extract the part with the category label of table as an image, then use the image to table model to convert it into Excel table data, then read the table data into DataFrame, and then convert it into HTML format; S5. For text, if it is a standard PDF document, use a PDF parsing tool (such as pymupdf) to read it and retain the position information. If it is a non-standard PDF document, use a layout analysis model (such as the omdetv2-turbo model) to extract it and retain the position information. The position information is represented by the x, y coordinates of the upper left corner and the x, y coordinates of the lower right corner of the recognition box. S6. For the image description, the layout analysis model is used to extract it and the position information is retained, which is also represented by the upper left corner coordinates and the lower right corner coordinates of the recognition box; S7. Assemble the extracted image description and the image file path into a hyperlink in Markdown format, convert it into Markdown format together with the extracted text and table, and store it for reading by the RAG system. The extracted text, table and image description content are converted into Markdown format and stored together with the image file for reading by the RAG system. When the image is replaced with a placeholder, it is equivalent to the text being converted, and the placeholder is equivalent to a hyperlink. When the RAG document data is subsequently processed, the corresponding image file is automatically read and called after the defined image placeholder is read. Since each block (including text, placeholder equivalent to the image, image description, and table) retains the position information, the image description and placeholder in the final Markdown format file also retain the corresponding relationship, thereby ensuring that there is no correspondence due to loss of information.

[0007] For table data, it may contain some information about merged cells. The HTML format can extract the data of merged cells, locate whether the current cell is a merged cell, and identify how many rows or columns it spans. This can be used as an intermediate state to ensure that there is no data loss, and then the data of the merged cells is supplemented to ensure the consistency and readability of the table data in a plain text environment. However, the markdown format cannot obtain the merged cell data. If it is directly converted to markdown, the information of the merged cells will be lost, resulting in inconsistency between the source data and the converted data, thus causing data loss. If the HTML format is used directly, because the table data using HTML contains a lot of Redundant information such as text will occupy a large number of characters in a single chunk when used in the RAG system, and will affect the recall efficiency and accuracy of the overall text. Therefore, the markdown format, which is pure text and does not lose the layout format, is better. It can provide the RAG system with a unified format and easy-to-process text input, greatly reducing the loss of information and redundant and complex information.

[0008] Preferably, in step S4, drawing a table specifically comprises: S41, parsing the drawing instructions in the PDF file line by line through a PDF rendering tool, identifying horizontal lines and vertical lines, and determining the table boundaries; S42, obtaining the position of the table in the page according to the length of the line, the width and height of the PDF document and the starting coordinates of the drawing point; S43, reading text within the table boundary using a PDF parsing tool; S44. Draw a table based on the table boundary, the lines within the table boundary, and the text within the table range.

[0009] Preferably, in step S41, the table boundary is determined by the following method: S411, reading out all horizontal and vertical lines in the current PDF document, including length and position information; S412, according to the relative horizontal coordinates and vertical coordinates, find the outermost horizontal and vertical lines as possible table boundaries; S413, for all possible table boundaries, select a line and determine whether it intersects with other lines one by one. If it does not intersect with any other lines, directly remove it. If it intersects with a certain line, determine whether it continues to extend after the intersection. If it does not extend, retain it. If both lines extend after the intersection, remove both lines. If one line extends after the intersection and the other does not extend, remove the line that does not extend and retain the other line. S414, repeat step S413 until all the retained lines have found intersections between each other, and the line that is finally retained is the table boundary.

[0010] The substantial effect brought by the present invention is that it provides the RAG system with text input in a unified format and easy to process, greatly reducing information loss. It not only solves the problem of existing PDF parsing tools in processing complex content, but also provides the RAG system with an efficient and accurate PDF content extraction method, thereby improving system performance and data processing reliability. BRIEF DESCRIPTION OF THE DRAWINGS

[0011] Figure 1 It is a flow chart of a PDF element extraction method based on a rendering process applicable to RAG of the present invention. DETAILED DESCRIPTION

[0012] The technical solution of the present invention is further specifically described below through embodiments and in conjunction with the accompanying drawings.

[0013] Embodiment: A PDF element extraction method based on rendering process applicable to RAG, such as Figure 1 As shown, the following steps are included: S1. Perform object detection on each page of the PDF file and decompose the content into several blocks. Each block is surrounded by a recognition box. Each recognition box is labeled according to the recognized category, which includes text, picture, table and picture description. S2. Recognition frame calibration: For standard PDF documents, the PDF tool is used to parse and read the category of each block in the formatted output process, and compare it with the category obtained in step S1. If there is a difference, the PDF formatted output shall prevail. For non-standard PDF documents, the recognition frame is enlarged by 1.05 times. The text in the standard document can be selected and copied, and the copied text is consistent with the text in the PDF. On the contrary, non-standard documents are generally scanned images, which are essentially images and cannot be selected and copied.

[0014] S3, Image data extraction: For images, first replace the area where the image exists with a unique placeholder, such as<image_x> , x is the serial number of the current image in the current PDF file, and the image in this area is saved with a unique placeholder as the file name, that is, saved as image_x.jpg; the traditional processing method cannot handle image data and can only ignore it. This solution replaces it with a placeholder. When the placeholder is read, the corresponding image file is retrieved, thereby retaining the position and reference value of the image in the document; S4. Table data extraction: Due to the characteristics of PDF as a descriptive language, the presentation of a table is usually achieved by drawing a series of lines and text blocks, rather than by definition like HTML. For a standard PDF document, draw a table and convert it into an image, then use the image-to-table model to convert the table's image data into Excel's table data, then read the table data into a DataFrame, and then convert it into HTML format. For non-standard PDF documents, directly extract the part with the category label of table as an image, then use the image to table model to convert it into Excel table data, then read the table data into DataFrame, and then convert it into HTML format; S5. Text extraction: For text, if it is a standard PDF document, use PDF parsing tools (such as pymupdf) to read it and retain the position information. If it is a non-standard PDF document, use layout analysis model (such as omdetv2-turbo model) to extract it and retain the position information. The position information is represented by the upper left corner x, y coordinates and the lower right corner x, y coordinates of the recognition box; S6, image description extraction: For the image description, the layout analysis model is used to extract it, and the position information is retained, which is also represented by the upper left corner coordinates and the lower right corner coordinates of the recognition box; S7, converting the extracted text, table and picture description content into Markdown format, and storing them together with the picture files for the RAG system to read. When the RAG document data is subsequently processed, the placeholders of the defined pictures are read and automatically replaced with the corresponding picture files.

[0015] For table data, it may contain some information about merged cells. The HTML format can extract the data of merged cells, locate whether the current cell is a merged cell, and identify how many rows or columns it spans. This can be used as an intermediate state to ensure that there is no data loss, and then the data of the merged cells is supplemented to ensure the consistency and readability of the table data in a plain text environment. However, the markdown format cannot obtain the merged cell data. If it is directly converted to markdown, the information of the merged cells will be lost, resulting in inconsistency between the source data and the converted data, thus causing data loss. If the HTML format is used directly, because the table data using HTML contains a lot of Redundant information such as text will occupy a large number of characters in a single chunk when used in the RAG system, and will affect the recall efficiency and accuracy of the overall text. Therefore, the markdown format, which is pure text and does not lose the layout format, is better. It can provide the RAG system with a unified format and easy-to-process text input, greatly reducing the loss of information and redundant and complex information.

[0016] In step S4, drawing a table specifically includes: S41, parsing the drawing instructions in the PDF file line by line through a PDF rendering tool, identifying horizontal lines and vertical lines, and determining the table boundaries; S42, obtaining the position of the table in the page according to the length of the line, the width and height of the PDF document and the starting coordinates of the drawing point; S43, reading text within the table boundary using a PDF parsing tool; S44. Draw a table based on the table boundary, the lines within the table boundary, and the text within the table range.

[0017] In step S41, the table boundary is determined by the following method: S411, reading out all horizontal and vertical lines in the current PDF document, including length and position information; S412, according to the relative horizontal coordinates and vertical coordinates, find the outermost horizontal and vertical lines as possible table boundaries; S413, for all possible table boundaries, select a line and determine whether it intersects with other lines one by one. If it does not intersect with any other lines, directly remove it. If it intersects with a certain line, determine whether it continues to extend after the intersection. If it does not extend, retain it. If both lines extend after the intersection, remove both lines. If one line extends after the intersection and the other does not extend, remove the line that does not extend and retain the other line. S414, repeat step S413 until all the retained lines have found intersections between each other, and the line that is finally retained is the table boundary.

[0018] The specific embodiments described herein are merely examples of the spirit of the present invention. Those skilled in the art may make various modifications or additions to the specific embodiments described or replace them in similar ways, but they will not deviate from the spirit of the present invention or exceed the scope defined by the appended claims.

[0019] Although the terms such as block, identification box, placeholder, etc. are used more frequently in this article, the possibility of using other terms is not excluded. These terms are used only to more conveniently describe and explain the essence of the present invention; interpreting them as any additional restrictions is contrary to the spirit of the present invention.

Claims

1. A PDF element extraction method based on rendering process suitable for RAG, characterized in that: The following steps are involved: S1. Detect objects on each page of the PDF file and decompose the content into several blocks. Each block is surrounded by a recognition box. Each recognition box is labeled according to the recognized category, which includes text, picture, table and picture description. S2. For standard PDF documents, the PDF tool is used to parse and read the category of each block in the formatted output process, and the category is compared with the category obtained in step S1. If there is a difference, the PDF formatted output shall prevail; for non-standard PDF documents, the identification box is enlarged by 1.05 times; S3. For the picture, first replace the place where the picture in the area exists with a unique placeholder, and save the picture in this area with the unique placeholder as the file name; S4. For a standard PDF document, draw a table and convert it into an image. Then use the image-to-table model to convert the image data of the table into Excel table data. Then read the table data into a DataFrame and convert it into HTML format. For non-standard PDF documents, directly extract the part with the category label of table as an image, then use the image to table model to convert it into Excel table data, then read the table data into DataFrame, and then convert it into HTML format; S5. For text, if it is a standard PDF document, use a PDF parsing tool to read it and retain the position information. If it is a non-standard PDF document, use a layout analysis model to extract it and retain the position information. S6. For the image description, use the layout analysis model to extract it and retain the position information; S7. Assemble the extracted image description and the image file path into a hyperlink in Markdown format, convert it into Markdown format together with the extracted text and table, and store it for reading by the RAG system.

2. The PDF element extraction method based on rendering process applicable to RAG according to claim 1, characterized in that: In step S4, drawing a table specifically includes: S41, parsing the drawing instructions in the PDF file line by line through a PDF rendering tool, identifying horizontal lines and vertical lines, and determining the table boundaries; S42, obtaining the position of the table in the page according to the length of the line, the width and height of the PDF document and the starting coordinates of the drawing point; S43, reading text within the table boundary range by using a PDF parsing tool; S44. Draw a table based on the table boundary, the lines within the table boundary, and the text within the table range.

3. The PDF element extraction method based on rendering process applicable to RAG according to claim 2, characterized in that: In step S41, the table boundary is determined by the following method: S411, reading out all horizontal and vertical lines in the current PDF document, including length and position information; S412, according to the relative horizontal coordinates and vertical coordinates, find the outermost horizontal and vertical lines as possible table boundaries; S413, for all possible table boundaries, select a line and determine whether it intersects with other lines one by one. If it does not intersect with any other lines, directly remove it. If it intersects with a certain line, determine whether it continues to extend after the intersection. If it does not extend, retain it. If both lines extend after the intersection, remove both lines. If one line extends after the intersection and the other does not extend, remove the line that does not extend and retain the other line. S414, repeat step S413 until all the retained lines have found intersections between each other, and the line that is finally retained is the table boundary.

Citation Information

Cited By

  • Document processing method and device, equipment and medium

    CN120145997A

  • Text retrieval method and device based on generative retrieval model, equipment and medium

    CN121301379A