pmaupin / pdfrw

pdfrw is a pure Python library that reads and writes PDFs
Other
1.84k stars 273 forks source link

PDF form filled with pdfrw can't be opened in adobe reader? #213

Open kbots-tech opened 3 years ago

kbots-tech commented 3 years ago

I'm working on a dungeons and dragons character sheet generator and this library has been a huge help though I've ran into one issue, with some applications either being unable to open the PDF or not showing the filled fields as is listed below: Blank on: MS Word for Android, MS Word for Win10, Adobe Acrobat for Android Can't open on: Adobe Reader for Win10 Works fine on: Opera for Win10, Chrome for Win10, Drive PDF Viewer for Android, OneDrive PDF Viewer for Android, Edge on Win10

Has anyone else has similar issues and is there a fix for this? The attached PDF is the one I'm exporting.

5eSheetOutput.pdf

Sefriol commented 3 years ago

Can you share the code you used to generate these forms?

I just fought a day to get field inputs to show in Adobe in custom PDFs I created, so there are some tricks that might help.

eric-d-rachell commented 3 years ago

Can you share the code you used to generate these forms?

I just fought a day to get field inputs to show in Adobe in custom PDFs I created, so there are some tricks that might help.

Is it possible for you to elaborate on any of these 'tricks' abstractly? I am having issues on chrome on Win10, android, and a number of other readers. Google searches show that this very general 'problem' has been recurrent in some way or form for years now, and spans a range of systems and readers. Any ideas?

kbots-tech commented 3 years ago

Can you share the code you used to generate these forms?

I just fought a day to get field inputs to show in Adobe in custom PDFs I created, so there are some tricks that might help.

Sorry for late getting back to you, this is the code I'm using to fill it, I didn't make this form/pdf file though. it's one I got from a friend slightly edited from the D&D official one, the function is pasted below or can also be seen in the file on line 637 https://github.com/mcurranseijo/Dungeons-and-Dragons-Bot/blob/main/cogs/charactersheet.py


async def fill_pdf(fields, in_file=None, out_file=None):
        if in_file is None:
            in_file = pdfrw.PdfReader('./5eSheet.pdf')
        if out_file is None:
            out_file = f'./5eSheetOutput.pdf'
        in_file.Root.AcroForm.update({pdfrw.PdfName.NeedAppearances: True})
        for i in range(len(in_file.Root.AcroForm.Fields)):
            item = in_file.Root.AcroForm.Fields[i]
            if item.FT == '/Btn':
                if fields[i] != 'NO':
                    item.AS = 'Yes'
            else:
                if fields[i] != 'NO':
                    item.V = str(fields[i])
        else:
            pdfrw.PdfWriter().write(out_file, in_file)
        return out_file