Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize the codebase #85

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions pdfcompare.php → bin/pdfcompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,33 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use ddn\sapp\AlmostOriginalLogger;
use ddn\sapp\PDFDoc;
use function ddn\sapp\helpers\p_debug_var;
use function ddn\sapp\helpers\p_debug;
use ddn\sapp\pdfvalue\PDFValueObject;

require_once('vendor/autoload.php');

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

$doc1 = PDFDoc::from_string(file_get_contents($argv[1]));
if ($doc1 === false)
fwrite(STDERR, "failed to parse file " . $argv[1]);

$doc2 = PDFDoc::from_string(file_get_contents($argv[2]));
if ($doc2 === false)
fwrite(STDERR, "failed to parse file " . $argv[2]);

$differences = $doc1->compare($doc2);
foreach ($differences as $oid => $obj) {
print($obj->to_pdf_entry());
}

require_once __DIR__ . '/../vendor/autoload.php';

if ($argc !== 3) {
fwrite(STDERR, sprintf('usage: %s <filename> <rev>', $argv[0]));
exit(1);
}

if (! file_exists($argv[1])) {
fwrite(STDERR, 'failed to open file ' . $argv[1]);
exit(1);
}

if (! file_exists($argv[2])) {
fwrite(STDERR, 'failed to open file ' . $argv[2]);
exit(1);
}

$doc1 = PDFDoc::from_string(file_get_contents($argv[1]));
$doc1->setLogger(new AlmostOriginalLogger());

$doc2 = PDFDoc::from_string(file_get_contents($argv[2]));
$doc2->setLogger(new AlmostOriginalLogger());

$differences = $doc1->compare($doc2);
foreach ($differences as $obj) {
print $obj->to_pdf_entry();
}
82 changes: 82 additions & 0 deletions bin/pdfdeflate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
This file is part of SAPP

Simple and Agnostic 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\AlmostOriginalLogger;
use ddn\sapp\PDFDoc;

require_once __DIR__ . '/../vendor/autoload.php';

if ($argc < 2 || $argc > 3) {
fwrite(STDERR, sprintf('usage: %s <filename> [oid]', $argv[0]));
exit(1);
}

if (! file_exists($argv[1])) {
fwrite(STDERR, 'failed to open file ' . $argv[1]);
exit(1);
}

$doc = PDFDoc::from_string(file_get_contents($argv[1]));
$doc->setLogger(new AlmostOriginalLogger());

$toid = null;
if ($argc === 3) {
$toid = (int) $argv[2];
}

foreach ($doc->get_object_iterator() as $oid => $object) {
if ($toid !== null && $oid !== $toid) {
continue;
}

if ($object === false) {
continue;
}

if ($object['Filter'] === '/FlateDecode' && $object['Subtype'] !== '/Image') {
$stream = $object->get_stream(false);
if ($stream !== false) {
unset($object['Filter']);
$object->set_stream($stream, false);
$doc->add_object($object);
}
}

// Not needed because we are rebuilding the document
if ($object['Type'] === '/ObjStm') {
$object->set_stream('', false);
$doc->add_object($object);
}

// Do not want images to be uncompressed
if ($object['Subtype'] === '/Image') {
$object->set_stream('');
$doc->add_object($object);
}

if ($toid !== null) {
print $object->get_stream(false);
}
}

if ($toid === null) {
echo $doc->to_pdf_file_s(true);
}
36 changes: 18 additions & 18 deletions pdfrebuild.php → bin/pdfrebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use ddn\sapp\AlmostOriginalLogger;
use ddn\sapp\PDFDoc;

require_once('vendor/autoload.php');

if (($argc < 2) || ($argc > 3))
fwrite(STDERR, sprintf("usage: %s <filename> [<output>]", $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 {
if ($argc == 3)
file_put_contents($argv[2], $obj->to_pdf_file_s(true));
else
echo $obj->to_pdf_file_s(true);
}
require_once __DIR__ . '/../vendor/autoload.php';

if ($argc < 2 || $argc > 3) {
fwrite(STDERR, sprintf('usage: %s <filename> [<output>]', $argv[0]));
exit(1);
}

if (! file_exists($argv[1])) {
fwrite(STDERR, 'failed to open file ' . $argv[1]);
} else {
$obj = PDFDoc::from_string(file_get_contents($argv[1]));
$obj->setLogger(new AlmostOriginalLogger());

if ($argc === 3) {
file_put_contents($argv[2], $obj->to_pdf_file_s(true));
} else {
echo $obj->to_pdf_file_s(true);
}
}
56 changes: 56 additions & 0 deletions bin/pdfsign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
This file is part of SAPP

Simple and Agnostic 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\AlmostOriginalLogger;
use ddn\sapp\PDFDoc;

require_once __DIR__ . '/../vendor/autoload.php';

if ($argc !== 3) {
fwrite(STDERR, sprintf('usage: %s <filename> <certfile>', $argv[0]));
exit(1);
}

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);
$obj->setLogger(new AlmostOriginalLogger());

if (! $obj->set_signature_certificate($argv[2], $password)) {
fwrite(STDERR, 'the certificate is not valid');
} else {
$docsigned = $obj->to_pdf_file_s();
if ($docsigned === false) {
fwrite(STDERR, 'could not sign the document');
} else {
echo $docsigned;
}
}
}
93 changes: 93 additions & 0 deletions bin/pdfsigni.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env php
<?php
/*
This file is part of SAPP

Simple and Agnostic 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\AlmostOriginalLogger;
use ddn\sapp\PDFDoc;
use ddn\sapp\PDFException;

require_once __DIR__ . '/../vendor/autoload.php';

if ($argc !== 4) {
fwrite(STDERR, sprintf('usage: %s <filename> <image> <certfile>', $argv[0]));
exit(1);
}

if (! file_exists($argv[1])) {
fwrite(STDERR, 'failed to open file ' . $argv[1]);
exit(1);
}

// 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);
$obj->setLogger(new AlmostOriginalLogger());

$position = [];
$image = $argv[2];
$imagesize = getimagesize($image);
if ($imagesize === false) {
fwrite(STDERR, 'failed to open the image ' . $image);

exit(1);
}

$pagesize = $obj->get_page_size(0);
if ($pagesize === false) {
return throw new PDFException('failed to get page size');
}

$pagesize = explode(' ', $pagesize[0]->val());
// Calculate the position of the image according to its size and the size of the page;
// the idea is to keep the aspect ratio and center the image in the page with a size
// of 1/3 of the size of the page.
$p_x = (int) ('' . $pagesize[0]);
$p_y = (int) ('' . $pagesize[1]);
$p_w = (int) ('' . $pagesize[2]) - $p_x;
$p_h = (int) ('' . $pagesize[3]) - $p_y;
[$i_w, $i_h] = $imagesize;

$ratio_x = $p_w / $i_w;
$ratio_y = $p_h / $i_h;
$ratio = min($ratio_x, $ratio_y);

$i_w = $i_w * $ratio / 3;
$i_h = $i_h * $ratio / 3;
$p_x = $p_w / 3;
$p_y = $p_h / 3;
// Set the image appearance and the certificate file
$obj->set_signature_appearance(0, [$p_x, $p_y, $p_x + $i_w, $p_y + $i_h], $image);
if (! $obj->set_signature_certificate($argv[3], $password)) {
fwrite(STDERR, 'the certificate is not valid');
} else {
$docsigned = $obj->to_pdf_file_s();
if ($docsigned == false) {
fwrite(STDERR, 'could not sign the document');
} else {
echo $docsigned;
}
}
Loading