Skip to main content

A package for low-level PDF crafting

Project description


PdfBuilder is a Python library for low-level PDF crafting. It allows:

  • creating new PDF files from list of COS objects;
  • adding new objects and editing existing ones via incremental saves;
  • working with both old xref tables and new xref stream objects.

How to install

$ pip install pdfbuilder

PdfBuilder is written in a pure Python and doesn't have any external dependencies. It requires Python 3.5 or higher to function properly.


A minimalistic code sample that creates "Hello, world" PDF file is listed below:

import os, zlib
from pdfbuilder import PDF, Object, ObjectReference

# (0) Open new, empty file
if os.path.isfile('test.pdf'):
doc = PDF('test.pdf')

# (1) Add pages object to PDF
pages_object = Object({
    'Type': '/Pages',
    'Kids': [ObjectReference(5)],  # We need to know page id beforehand :(
    'Count': 1,
    'MediaBox': [0, 0, 595.28, 841.89]

# (2) Add font to pdf
font_object = Object({
    'Type': '/Font',
    'BaseFont': '/Helvetica',
    'Subtype': '/Type1',
    'Encoding': '/WinAnsiEncoding'

# (3) Add info object to PDF
pdf_info_object = Object({
    'ProcSet': ['/PDF', '/Text', '/ImageB', '/ImageC', '/ImageI'],
    'Font': [Object({'F1': font_object.ref()})]

# (4) Add first page content to PDF
content = b'2 J\n0.57 w\nBT /F1 14.00 Tf ET\nBT 56.69 785.20 Td (Hello, world!) Tj ET\n'
content_compressed = zlib.compress(content)
content_object = Object({
    'Filter': '/FlateDecode',
    'Length': len(content_compressed)
}, stream_data=content_compressed)

# (5) Add first page to PDF
page_object = Object({
    'Type': '/Page',
    'Parent': pages_object.ref(),
    'Resources': pdf_info_object.ref(),
    'Contents': content_object.ref()

# (6) Add main catalog to PDF
catalog_object = Object({
    'Type': '/Catalog',
    'Pages': pages_object.ref(),
    'OpenAction': [page_object.ref(), '/FitH', 'null'],
    'PageLayout': '/OneColumn'

# (7) Add xref table object and save PDF, info_id=2)

The following example shows how to add another page to newly created document. Updates in PDF structure will be saved incrementally.

from pdfbuilder import PDF, Object, ObjectReference

# (0) Open already existing PDF
doc = PDF('test.pdf')

# (1) Add new page to PDF
second_page_object = Object({
    'Type': '/Page',
    'Parent': ObjectReference(1),
    'Resources': ObjectReference(3),
    'Contents': ObjectReference(4)

# (2) Update pages object
new_pages_object = Object({
    'Type': '/Pages',
    'Kids': [ObjectReference(5), second_page_object.ref()],
    'Count': 2,
    'MediaBox': [0, 0, 595.28, 841.89]
}, id=1)
doc.update_object(new_pages_object, 1)

# (3) Add new xref table object and incrementally update PDF, info_id=2)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdfbuilder-0.1.0.tar.gz (4.7 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page