0

I'm using Python 3.10 on Windows server 2019 standard to fill PDF form then flatten it then email it as attachment.

I followed the instructions here Generate flattened PDF with Python. All options worked in the most part, I was able to open PDF in Firefox, Chrome and in Adobe reader and Adobe document cloud. I used Adobe Acrobat DC Standard to create simple form. The last code I tried is from Charalamm in the link above. The screen shots below are after trying Charalamm code. (Other options has similar if not the same experience)

However, the preview PDF handler in Outlook client (Microsoft® Outlook® for Microsoft 365), outlook.office.com. is not showing data or values in the fields. It shows the text fields. In office 365 one drive, the PDF preview shows data but the font are huge and cannot be changed even if I edit PDF and make it the smallest possible.

Does anyone know why PDF Preview is not working? Is this MS issue?

Outlook client PDF preview enter image description here enter image description here

One Drive PDF preview works but its ignores font size enter image description here

Outlook office 365 web client enter image description here

Dumping out Fields using pdfrw package

import pdfrw

pdf_obj = pdfrw.PdfReader(fillable_file) 

for x in pdf_obj.Root.AcroForm.Fields:
      print(x)

{'/DA': '(/Helv 8 Tf 0 0 1 rg)', '/F': '4', '/FT': '/Tx', '/Ff': '1', '/MK': {}, '/P': (17, 0), '/Rect': ['147.989', '735.367', '297.989', '757.367'], '/Subtype': '/Widget', '/T': '(Text1)', '/Type': '/Annot', '/V': 'This is sample text', '/AP': ''}
dkgcb
  • 81
  • 1
  • 2
  • 9

1 Answers1

0

Just in case someone else has similar problem, I'm going to add my solution to this problem. The solution I found to be solid pdf library (FPDF Tutorial) called

from fpdf import FPDF

which requires a bit more legwork but the output is the PDF file that looks exactly the same on most devices I was able to test. I also used PyPDF

from PyPDF2 import PdfMerger

To merge the generated PDF files.

It all depends on how you get the data, in my example I had a report with row - for each department across, and the columns where types of the gifts with totals at the end as last column. Also all the last row was total for all departments for the particular gift type. Data was coming in already calculated - before report was generated.

Ex.

# Gift Report
# Headers across the top by type of the gift
# Headers in the first column indicating Departments
# Totals in the last row
# Totals in the last column

# Set PDF properties and insert the background image
pdf = FPDF(orientation = 'L', unit = 'mm', format = 'A4')
pdf.add_page()
pdf.image(self.backgrnd_image, x = 60, y = 60, w = 211, h = 100)

# Add custom fonts - You can use google fonts in your app
pdf.add_font('Lora',
                '',
                self.path_to_fonts + "Lora\static\Lora-Regular.ttf",
                uni=True)
pdf.add_font('LoraItalic',
                '',
                self.path_to_fonts + "Lora\static\Lora-Italic.ttf",
                uni=True)


# TITLE CELL
pdf.set_fill_color(214, 213, 210)
pdf.set_text_color(0,0,0)
pdf.set_font("Lora", size=12, style='U')
pdf.set_xy(10,26)
pdf.cell(0, 12.5, txt='Gift Report', ln=1, align="C" , fill = False)

# SUB TITLE
pdf.set_font("Lora", size=10, style='I')
pdf.set_xy(10,36)
pdf.cell(0, 12.5, txt='This is sample SUBTITLE.', ln=1, align="C" , fill = False)

# REPORT YEAR
pdf.set_font("LoraItalic", size=10, style='B')
pdf.set_xy(135,43)
pdf.cell(10, 10, txt=str(fiscal_year), ln=0, align="C" , fill = False)


# SET UP COLUMN HEADERS: Gift 1, Gift 2... Total by Unit
set_y = 53

pdf.set_font("Arial", size=9, style='B')
pdf.set_xy(70,set_y)
pdf.cell(10, 10, txt="Gift Type 1", ln=0, align="C" , fill = False)

pdf.set_font("Arial", size=9, style='B')
pdf.set_xy(97,set_y)
pdf.cell(10, 10, txt="Gift Type 2", ln=0, align="C" , fill = False)

pdf.set_font("Arial", size=9, style='B')
pdf.set_xy(251,set_y)
pdf.cell(10, 10, txt="Total by Dept", ln=0, align="C" , fill = False)

...


# ROWS - Header names (In first column) on the left

set_y = 60
for row in gift_data:

    # Put space between rows based on the index/position of the element to lay it accurately as possible over image background
    if row[7] == 9: #Dept 1
      set_y = set_y + 6.1

    if row[7] == 13: #Dept 2
      set_y = set_y + 6.1

    if row[7] == 14: #Dept 3
      set_y = set_y + 6.5


# ROWS
pdf.set_font("Arial", size=8, style='')
pdf.set_xy(82,set_y)
pdf.cell(10, 10, txt="$" + "{:,.2f}".format(row[1]), ln=0,  align="R" , fill = False)

pdf.set_font("Arial", size=8, style='')
pdf.set_xy(105,set_y)
pdf.cell(10, 10, txt="$" + "{:,.2f}".format(row[2]), ln=0, align="R" , fill = False)

pdf.set_font("Arial", size=8, style='')
pdf.set_xy(105,set_y)
pdf.cell(10, 10, txt="$" + "{:,.2f}".format(row[2]), ln=0, align="R" , fill = False)

    ...
dkgcb
  • 81
  • 1
  • 2
  • 9