PdfBuilder
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.
Example
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'):
os.remove('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]
})
doc.add_new_object(pages_object)
# (2) Add font to pdf
font_object = Object({
'Type': '/Font',
'BaseFont': '/Helvetica',
'Subtype': '/Type1',
'Encoding': '/WinAnsiEncoding'
})
doc.add_new_object(font_object)
# (3) Add info object to PDF
pdf_info_object = Object({
'ProcSet': ['/PDF', '/Text', '/ImageB', '/ImageC', '/ImageI'],
'Font': [Object({'F1': font_object.ref()})]
})
doc.add_new_object(pdf_info_object)
# (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)
doc.add_new_object(content_object)
# (5) Add first page to PDF
page_object = Object({
'Type': '/Page',
'Parent': pages_object.ref(),
'Resources': pdf_info_object.ref(),
'Contents': content_object.ref()
})
doc.add_new_object(page_object)
# (6) Add main catalog to PDF
catalog_object = Object({
'Type': '/Catalog',
'Pages': pages_object.ref(),
'OpenAction': [page_object.ref(), '/FitH', 'null'],
'PageLayout': '/OneColumn'
})
doc.add_new_object(catalog_object)
# (7) Add xref table object and save PDF
doc.save(root_id=6, 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)
})
doc.add_new_object(second_page_object)
# (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
doc.save(root_id=6, info_id=2)
Metadata
Release files for pdfbuilder 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pdfbuilder-0.1.0.tar.gz | 4.7 kB | Without provenance | Details for pdfbuilder-0.1.0.tar.gz |
Release files / pdfbuilder-0.1.0.tar.gz
| Download URL | pdfbuilder-0.1.0.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e63b86f9413712d7ae7c6b1c5b031bfe3676a43cae689ab99995b22935472128
|
|
BLAKE2b-256 checksum How to use checksums |
0e5f9180f6fcd4a1c20f7de86df5e9d23049606b9d3aeb158dedc1529793e4ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.4.2 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.5
|
Security for pdfbuilder 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.