Issues Creating a PDF Generator in Python

87 views Asked by At

My goal with this program is to auto-generate blank purchase order forums for my company using Python. I am using the pdfrw module to open a pdf template, fill in the first fillable cell of the pdf with the purchase order number (or PO number) and make that value read-only. The code I currently have has no errors and does generate PDFs, however the resulting PDFs appear unchanged until I open one of them in Adobe Acrobat and click on the field I attempted to fill. When I click on the field the number appears, but when I close the pdf and reopen it the number disappears.

Here is the code I am working with:

# Import the necessary modules
from pdfrw import PdfName, PdfReader, PdfWriter

# Open the input PDF file
input_file = open('digitalPOTemplate.pdf', 'rb')

# Create a PdfReader object for the input file
reader = PdfReader(input_file)

start = 211250
end = 211274
# Loop through the numbers in the range
for num in range(start, end+1):
    # Create a PdfWriter object for the output file
    writer = PdfWriter()

    # Loop through the pages in the input file
    for page in reader.pages:
        # Find the first fillable cell on the page
        fillable_cell = page.Annots[0]
        # Fill in the number in the cell
        fillable_cell.update({PdfName('V'): str(num)})

         # Make the fillable cell visible
        fillable_cell.update({PdfName('Ff'): 1})

        # Make the filled-in text read-only
        fillable_cell.update({PdfName('/Ff'): PdfName('/ReadOnly')})    

        # Update the appearance stream
        fillable_cell.update({PdfName('/AP'): None})

        # Add the updated page to the output file
        writer.addpage(page)

    # Create the output file name
    output_file_name = 'PO-' + str(num) + '-BLANK.pdf'
    # Write the output file
    writer.write(output_file_name)

# Close the input file
input_file.close()

Here is an example of a resulting file: https://drive.google.com/file/d/1kD9vRqrAVmse-6nkS6uZsfgAg1GnHfZ7/view?usp=sharing

If you see in the upper right hand corner I am trying to change the "NO." value.

Does anyone have any ideas on how to fix this?

1

There are 1 answers

2
Space134 On

enter image description here

I reask ChatGPT, and what I got:
"""If you have a PDF file and you're unable to find certain text using pdfrw.PdfReader, it's likely because the text is embedded as an image or is part of the PDF content in a way that pdfrw cannot easily extract it as text. pdfrw primarily deals with text that is stored as text objects within the PDF file"""

I also check your file by adding:

print(reader)

enter image description here As you can see there is not too much info here to manipulate. to check