Skip to content

PDF Font Autosize Not Working On Firefox  #2551

@kyl293

Description

@kyl293

What happened? What were you trying to achieve?
PDF's that are generated do not auto size font for inputs on Firefox but look normal on Microsoft Edge and Google Chrome. If I then save it using acrobat it fixes the font size issue. If there is a way (code wise) to fix the scaling issue?

Environment

Which environment were you using when you encountered the problem?
I am running the software on AWS Lambda (Put pypdf in a layer so that Lambda can access it)

$ python -m platform
{
  "statusCode": 200,
  "body": {
    "file_name": "1182_insert_id_card.pdf"
  }
}
# TODO: Your output goes here

Code + PDF

This is a minimal, complete example that shows the issue:

import os
import json
import boto3
from pypdf import PdfReader, PdfWriter
from io import BytesIO

def generate_pdf(event, context):
    # Extract input data from the Lambda event
    #print(json.dumps(event))
 # Retrieve the raw JSON payload from the request body
    raw_body = event.get("body")
    input_data = raw_body
    #print("A")
    # Check if the body is present
    """
    if raw_body is not None:
        
        # Parse the JSON from the request body
        try:
           input_data = json.loads(raw_body)
        except json.JSONDecodeError as e:
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'Invalid JSON payload'}),
            }
    else:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Request body is missing'}),
        }
"""
    # Now input_data should contain the parsed JSON payload
    #print(f"Received Input Data: {input_data}")

    # Path to the template PDF file (assuming it's included in the Lambda deployment package)
    #print("B")
    template_path = getTemplate(input_data)
    #print(f"{template_path=}")
    #print("C")
    # Fill in the form fields in the template PDF
    #print("d")
    filled_pdf_buffer = fill_pdf_template(template_path, input_data)
    #print("e")
    # Store the filled PDF in the /tmp directory

    # Upload the filled PDF to the destination S3 bucket
    #print('f')
    destination_bucket = input_data.get('dest_bucket')
    output_key = get_output_key(input_data)
    #print('g')
    upload_to_s3(filled_pdf_buffer, destination_bucket, output_key)
    #print('h')
    #print("b4 return")

    return {
            'statusCode': 200,
            'body': {'file_name': f'{filled_pdf_buffer.split("/")[-1]}'}
        }
        
        
def fill_pdf_template(template_path, input_data):

    reader = PdfReader(template_path,strict=True)
    metadata=reader.metadata
    writer = PdfWriter()
    writer.clone_reader_document_root(reader)
    writer.add_metadata(metadata)    
    pagecount=0
    writer.set_need_appearances_writer(False)
   
    
    writer.append(reader)
    
    for page in range(len(reader.pages)):
        fields = reader.get_form_text_fields()
        print(fields)
        if fields is not None:
            writer.update_page_form_field_values(
                writer.pages[page],
                input_data.get('input_data'),
                flags=1,
                auto_regenerate=True,
           )

    #print("run" ,page)
    # write "output" to pypdf-output.pdf
    tmp_filename=f'/tmp/{input_data.get("uniqueID", "Unknown")}_{input_data.get("file_name", "Unknown")}'
    with open(tmp_filename, "wb") as output_stream:
        writer.write(output_stream)
 
    return tmp_filename
    
    
def get_output_key(input_data):
    # Customize how you want to generate the output key based on input data
    # This is just a simple example, adjust it as needed
    return f'{input_data.get("uniqueID", "Unknown")}_{input_data.get("file_name", "Unknown")}'

def upload_to_s3(tmp_filename,dest_bucket,output_key):
    s3 = boto3.client('s3')
    s3.upload_file(tmp_filename,dest_bucket,output_key)


def getTemplate(input_data):
    s3= boto3.client('s3')
    bucket=input_data.get("bucket_name")
    print(f"{bucket=}")
    path=input_data.get("file_path")
    print(f"{path=}")
    name='/tmp/'+input_data.get("file_name")
    print(f"{name=}")
    try:
        print("yes")
        s3.download_file(bucket,path,name)
        return name
    except  Exception as error:
        print("error during getTemplate")
        print(str(error))
        return {
            'statusCode': 400,
            'body': {'error': error},
        }

Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!
Card with filled dummy data
1182_insert_id_card.pdf
Card with unfilled data
insert_id_card (1).pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    workflow-formsFrom a users perspective, forms is the affected feature/workflow

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions