Download the PHP package ddn/sapp without Composer
On this page you can find all versions of the php package ddn/sapp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sapp
SAPP - Simple and Agnostic PDF Document Parser
SAPP stands for Simple and Agnostic PDF Parser and it makes what is name says: parsing PDF files. It also enables other cool features such as rebuilding documents (to make the content more clear or compact) or digitally signing documents.
SAPP is agnostic because it does not care of composing PDF documents (e.g. adding pages, updating pages, etc.). Instead, its aim is to be somehow a backend to parse an existing PDF document and to manipulate the objects included on it, or to create new ones.
The way of working with SAPP can be seen in the function to sign the document: it is an independent function that adds and manipulates the PDF objects contained in the document.
Some of the features of SAPP:
- Supports 1.4 PDF documents
- Supports many features of 1.5 PDF and later documents (including cross-reference streams)
- Works using incremental versions
- Works for rebuilding documents to flatten versions (older version are dropped)
- Signature of documents using the Acrobat workflow (and obtain the green checkmark).
- Others.
1. Why SAPP
I created SAPP because I wanted to programmatically sign documents, including multiple signatures.
I tried tcpdf along with FPDI, but the results are not those expected. When importing a document using FPDI, the result is a new document with the pages of the original one, not the original document. So if the original document was signed, those signatures were lost.
I read about SetaPDF-Signer, but it cannot be used for free. I also inspected some CLI tools such as PortableSigner, but (1) it was Java based (and I really don't like java) and (2) it depends on iText and other libraries which don't seem to be free at this moment.
At the end I got the PDF 1.7 definition, and I learned about incremental PDF documents and its utility to include multiple signature in documents.
2. Using SAPP
2.1 Using composer and packagist
To use SAPP in your projects, you just need to use composer:
Then, a vendor
folder will be created, and then you can simply include the main file and use the classes:
2.2 Getting the source code
Altenatively you can clone the repository and use composer:
Then you will be ready to include the main file and use the classes.
3. Examples
In the root folder of the source code you can find two simple examples:
pdfrebuild.php
: this example gets a PDF file, loads it and rebuilds it to make every PDF object to be in order, and also reducing the amount of text to define the document.pdfsign.php
: this example gets a PDF file and digitally signs it using a pkcs12 (pfx) certificate.pdfsigni.php
: this example gets a PDF file and digitally signs it using a pkcs12 (pfx) certificate, and adds an image that makes visible the signature in the document.pdfsignx.php
: alternate example that gets a PDF file and digitally signs it using a pkcs12 (pfx) certificate, and adds an image that makes visible the signature in the document.pdfcompare.php
: this example compares two PDF files and checks the differences between them (object by object, field by field).
3.1. Rebuild PDF files with pdfrebuild.php
Once cloned the repository and generated the autoload content, it is possible to run the example:
The result is a more ordered PDF document which is (problably) smaller. (e.g. rebuilding examples/testdoc.pdf provides a 50961 bytes document while the original was 51269 bytes).
And the xref
table looks significantly better ordered:
The code:
3.2. Sign PDF files with pdfsign.php
To sign a PDF document, it is possible to use the script pdfsign.php
:
And now the document is signed. And if you wanted to add a second signature, it is as easy as signing the resulting document again:
The code: (full working example)
3.3. Sign PDF files with an image, using pdfsignx.php
To sign a PDF document that contains an image associated to the signature, it is possible to use the script pdfsignx.php
:
And now the document is signed, and a cool image appears. If you wanted to add a second signature, it is as easy as signing the resulting document again.
The code: (full working example)
3.4. Sign PDF files with an image, using pdfsigni.php
To sign a PDF document that contains an image associated to the signature, it is possible to use the script pdfsigni.php
:
And now the document is signed, and a cool image appears. If you wanted to add a second signature, it is as easy as signing the resulting document again.
The main difference with the previous code is that in this case, the signature is considered a stage of editing the document, but it will not be signed until the document is generated. So it is possible to edit the document (i.e. add text or images) and defer the signature until the document is finally generated.
The code:
* the code related to the position in which the signature and the image appear has been ommited, but can be seen in file pdfsigni.php
.
3.5. Decompress the streams of any object with pdfdeflate.php
This is an example of how to manipulate the objects in a document. The example walks over any object in the document, obtains its stream deflated, removes the filter (i.e. compression method), and restores it to the document. So that the objects are not compressed anymore (e.g. you can read in plain text the commands used to draw the elements).
The code:
4. Limitations
At this time, the main limitations are:
- Basic support for non-zero generation pdf objects: they are uncommon, but according to the definition of the PDF structure, they are possible. If you find one non-zero generation object please send me the document and I'll try to support it.
- Not dealing with encrypted documents.
- Other limitations, for sure :)
5. Future work
My idea is to provide support for other cool features:
- Support for TSA timestamping
- Include document protection (i.e. lock documents)
- Document encryption (http://www.fpdf.org/en/script/script37.php)
- Try to provide support to write some text
6. Attributions
- The mechanism for calculating the signature hash is heavily inspired in tcpdf.
- Reading jpg and png files has been taken from fpdf.