Skip to content

Commit

Permalink
first public version
Browse files Browse the repository at this point in the history
  • Loading branch information
dealfonso committed Nov 5, 2020
1 parent 68afa75 commit 1b938e8
Show file tree
Hide file tree
Showing 25 changed files with 3,702 additions and 2 deletions.
165 changes: 165 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

0. Additional Definitions.

As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".

The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

1. Exception to Section 3 of the GNU GPL.

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

2. Conveying Modified Versions.

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or

b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.

3. Object Code Incorporating Material from Library Header Files.

The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the object code with a copy of the GNU GPL and this license
document.

4. Combined Works.

You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.

c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.

d) Do one of the following:

0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.

1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.

e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)

5. Combined Libraries.

You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.

b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.

6. Revised Versions of the GNU Lesser General Public License.

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
163 changes: 161 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,161 @@
# sapp
Simply a PDF Document Parser in PHP
# SAPP - Simply A PDF Document Parser

SAPP stands for Simply a 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), adding images or digitally signing documents.

# 1. Why SAPP
I created SAPP because I wanted to programmatically sign documents, including **multiple signatures**.

I tried [tcpdf](https://tcpdf.org/) along with [FPDI](https://www.setasign.com/products/fpdi/downloads/), 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](https://www.setasign.com/products/setapdf-signer/details/), but it cannot be used for free. I also inspected some CLI tools such as [PortableSigner](https://github.com/pflaeging/PortableSigner2), 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](https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf), and I learned about incremental PDF documents and its utility to include multiple signature in documents.

# 2. Using SAPP

To test the examples, you can clone the repository and use [composer](https://getcomposer.org/):

```bash
$ git clone https://github.com/dealfonso/sapp
$ cd sapp
$ composer dump-autoload
$ php pdfrebuild.php examples/testdoc.pdf > testdoc-rebuilt.pdf
```

## 1.1. Examples

In the root folder you can find two simple examples:

1. `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.
1. `pdfsign.php`: this example gets a PDF file and digitally signs it using a pkcs12 (pfx) certificate.

### 2.1.1. Rebuild PDF files with `pdfrebuild.php`

Once cloned the repository and generated the autoload content, it is possible to run the example:

```bash
$ php pdfrebuild.php examples/testdoc.pdf > testdoc-rebuilt.pdf
```

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).

```
$ ls -l examples/testdoc.pdf
-rw-r--r--@ 1 calfonso staff 51269 5 nov 14:01 examples/testdoc.pdf
$ ls -l testdoc-rebuilt.pdf
-rw-r--r--@ 1 calfonso staff 50961 5 nov 14:22 testdoc-rebuilt.pdf
```

And the `xref` table looks significantly better ordered:
```
$ cat examples/testdoc.pdf
...
xref
0 39
0000000000 65535 f
0000049875 00000 n
0000002955 00000 n
0000005964 00000 n
0000000022 00000 n
0000002935 00000 n
0000003059 00000 n
0000005928 00000 n
...
$ cat testdoc-rebuilt.pdf
...
xref
0 8
0000000000 65535 f
0000000009 00000 n
0000000454 00000 n
0000000550 00000 n
0000000623 00000 n
0000003532 00000 n
0000003552 00000 n
0000003669 00000 n
...
```

**The code:**

```php
use ddn\sapp\PDFDoc;

require_once('vendor/autoload.php');

if ($argc !== 2)
fwrite(STDERR, sprintf("usage: %s <filename>", $argv[0]));
else {
if (!file_exists($argv[1]))
fwrite(STDERR, "failed to open file " . $argv[1]);
else {
$obj = PDFDoc::from_string(file_get_contents($argv[1]));

if ($obj === false)
fwrite(STDERR, "failed to parse file " . $argv[1]);
else
echo $obj->to_pdf_file_s(true);
}
}
```

### 2.1.2. Sign PDF files with `pdfsign.php`

To sign a PDF document, it is possible to use the script `pdfsign.php`:

```bash
$ php pdfsign.php examples/testdoc.pdf caralla.p12 > testdoc-signed.pdf
```

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:

```bash
$ php pdfsign.php testdoc-signed.pdf user.p12 > testdoc-resigned.pdf
```

**The code:**

```php
use ddn\sapp\PDFDoc;

require_once('vendor/autoload.php');

if ($argc !== 3)
fwrite(STDERR, sprintf("usage: %s <filename> <certfile>", $argv[0]));
else {
if (!file_exists($argv[1]))
fwrite(STDERR, "failed to open file " . $argv[1]);
else {
// Silently prompt for the password
fwrite(STDERR, "Password: ");
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
fwrite(STDERR, "\n");

$file_content = file_get_contents($argv[1]);
$obj = PDFDoc::from_string($file_content);

if ($obj === false)
fwrite(STDERR, "failed to parse file " . $argv[1]);
else {
if ($obj->sign_document($argv[2], $password) === false)
fwrite(STDERR, "could not sign the document");
else
echo $obj->to_pdf_file_s();
}
}
}
```

# 3. Limitations

At this time, the main limitations are:
- Not dealing with **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 (you get an exception or error in that sense), please send me the document and I'll try to support it.
- Not dealing with **encrypted documents**.
- Other limitations, for sure :)

# 4. Attributions

1. The mechanism for calculating the signature hash is heavily inspired in tcpdf.
1. Reading jpg and png files has been taken from fpdf.
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ddn/sapp",
"description": "Simply a PDF Parser",
"type": "library",
"license": "GNU/LGPLv3",
"authors": [
{
"name": "Carlos A.",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2"
},
"autoload": {
"psr-4": {"ddn\\sapp\\": "src/"}
}
}
Binary file added examples/testdoc.pdf
Binary file not shown.
39 changes: 39 additions & 0 deletions pdfrebuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
This file is part of SAPP
Simply A PDF Parser (SAPP) - Parse PDF documents in PHP (and update them)
Copyright (C) 2020 - Carlos de Alfonso ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use ddn\sapp\PDFDoc;

require_once('vendor/autoload.php');

if ($argc !== 2)
fwrite(STDERR, sprintf("usage: %s <filename>", $argv[0]));
else {
if (!file_exists($argv[1]))
fwrite(STDERR, "failed to open file " . $argv[1]);
else {
$obj = PDFDoc::from_string(file_get_contents($argv[1]));

if ($obj === false)
fwrite(STDERR, "failed to parse file " . $argv[1]);
else
echo $obj->to_pdf_file_s(true);
}
}
Loading

0 comments on commit 1b938e8

Please sign in to comment.