Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a PDF document
#1
what tools are best for a Python script to produce its output as a PDF document in latest version format (1.4)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
ReportLab is the most powerful and feature-rich option.
If need a simpler tool or are converting PDFs, Fpdf2 and PikePDF are good choices.

Samples.
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def create_pdf(output_filename):
    c = canvas.Canvas(output_filename, pagesize=letter)
    c.drawString(100, 750, "Hello, this is a PDF document!")
    c.save()

create_pdf("output.pdf")
from fpdf import FPDF

def create_pdf(output_filename):
    pdf = FPDF()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt="Hello, this is a PDF document!", ln=True)
    pdf.output(output_filename, version="1.4")

create_pdf("output.pdf")
import pikepdf

def convert_pdf_to_1_4(input_pdf, output_pdf):
    with pikepdf.open(input_pdf) as pdf:
        pdf.save(output_pdf, version=pikepdf.PdfVersion.v1_4)

convert_pdf_to_1_4("input.pdf", "output.pdf")
Skaperen likes this post
Reply
#3
I often used Python to generate LaTeX source code which can be compiled to pdf document. For the type of documents that I need to produce, this is an easy solution.

I don't have experience with generating specifically pdf 1.4 documents but I think using the pdf14 package in latex documents does the trick.
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
Hi, I use reportlab and also FPDF if I need to include a picture in the document.
Starts like this:
pdf1 = FPDF(orientation='P', unit='mm', format='A4')
        pdf1.add_page()
        pdf1.image(fotopath, x=10, y=15, w=50)
        pdf1.set_font('Helvetica', size=18)
        pdf1.cell(220, 10, txt='First line of text ...', ln=1, align="C")
etc...
FPDF produces pdf 1.3 , but since the years i've been using it, I suppose that version 1.4 must be possible.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
(Aug-15-2024, 06:51 AM)DPaul Wrote: Hi, I use reportlab and also FPDF if I need to include a picture in the document.
Starts like this:
pdf1 = FPDF(orientation='P', unit='mm', format='A4')
        pdf1.add_page()
        pdf1.image(fotopath, x=10, y=15, w=50)
        pdf1.set_font('Helvetica', size=18)
        pdf1.cell(220, 10, txt='First line of text ...', ln=1, align="C")
etc...
FPDF produces pdf 1.3 , but since the years i've been using it, I suppose that version 1.4 must be possible.
Paul
would i need to learn LaTeX to do more of this? i don't know how complicated the "etc..." part would be but the first 5 lines look guessable (i might want an American page size).

1.3 might work. i'll send a 1.3 to them to try it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020