Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filled PDF form fields not visible in Windows preview #246

Open
jrennemo77 opened this issue May 29, 2024 · 1 comment
Open

Filled PDF form fields not visible in Windows preview #246

jrennemo77 opened this issue May 29, 2024 · 1 comment

Comments

@jrennemo77
Copy link

I'm using pdfrw to fill out a PDF form. However, the filled values in the PDF's text input fields are not visible when I preview the file using Windows' built-in preview feature.
If I open the file in Adobe Acrobat and then save it (even without making any changes), the text fields become visible in Windows preview.
I would like the filled fields to be visible in Windows preview immediately after the file is generated.
I would appreciate any help. Thanks.

import pdfrw

ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'

def merge_pdfs(pdf_paths, output_path):
    writer = pdfrw.PdfWriter()
    
    for path in pdf_paths:
        reader = pdfrw.PdfReader(path)
        writer.addpages(reader.pages)
    writer.trailer.Root.AcroForm = pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))   
    writer.write(output_path)
    return output_path

def fill_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    for page in template_pdf.pages:
        annotations = page[ANNOT_KEY]
        if annotations:
            for annotation in annotations:
                if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY and annotation[ANNOT_FIELD_KEY]:
                    key = annotation[ANNOT_FIELD_KEY][1:-1]  
                    if key in data_dict:
                        if type(data_dict[key]) == bool:  # Hantering för kryssrutor
                            annotation.update(
                                pdfrw.PdfDict(V=pdfrw.PdfName('On' if data_dict[key] else 'Off'),
                                              AS=pdfrw.PdfName('On' if data_dict[key] else 'Off')))
                        else:  # Hantering för textfält
                            annotation.update(
                                pdfrw.PdfDict(V='{}'.format(data_dict[key])))
                            annotation.update(pdfrw.PdfDict(AP=''))

    if template_pdf.Root and '/AcroForm' in template_pdf.Root:
        template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
    
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)

def fill_simple_pdf_file(data, template_input, template_output):
    return fill_pdf(template_input, template_output, data)
@jiaotiliu
Copy link

Have the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants