Skip to content

Commit 80115f1

Browse files
committed
Re-optimizing a PDF should not change the file
1 parent 50eb792 commit 80115f1

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/Engine/Optimize/Pdftk.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,48 @@ public function convertFile($source, $destination) {
4949
$this->cmd,
5050
$source,
5151
'dump_data',
52-
Shell::arg('|', Shell::SHELL_SAFE),
52+
Shell::arg('2>&1 |', Shell::SHELL_SAFE),
5353
Shell::arg("sed 's/Value: .*/Value:/' > ", Shell::SHELL_SAFE),
5454
$empty,
5555
));
5656

57-
// pdftk source.pdf update_info empty.txt output destination.pdf flatten compress
58-
$cmd = array(
59-
$this->cmd,
60-
$source,
61-
'update_info',
62-
$empty,
63-
'output',
64-
'-',
65-
'flatten',
66-
($remove_meta ? 'uncompress' : 'compress'),
67-
);
68-
if (isset($this->configuration['remove-id'])
69-
&& $this->configuration['remove-id']) {
70-
// /ID [<e8c87bb7a19df73c042da3b2a01dc7ff> <e8c87bb7a19df73c042da3b2a01dc7ff>]
71-
$cmd[] = Shell::arg(" | grep -va '^\\/ID \\[' ", Shell::SHELL_SAFE);
57+
// Determine whether InfoKey exists.
58+
$infoKey = preg_match("@(?:^|\n)InfoKey:@s", file_get_contents($empty));
59+
if ($infoKey || $remove_meta) {
60+
// pdftk source.pdf update_info empty.txt output destination.pdf flatten compress
61+
$cmd = array(
62+
$this->cmd,
63+
$source,
64+
'update_info',
65+
$empty,
66+
'output',
67+
'-',
68+
'flatten',
69+
($remove_meta ? 'uncompress' : 'compress'),
70+
);
71+
if (isset($this->configuration['remove-id'])
72+
&& $this->configuration['remove-id']) {
73+
// /ID [<e8c87bb7a19df73c042da3b2a01dc7ff> <e8c87bb7a19df73c042da3b2a01dc7ff>]
74+
$cmd[] = Shell::arg(" | grep -va '^\\/ID \\[' ", Shell::SHELL_SAFE);
75+
}
76+
$cmd[] = Shell::arg('>', Shell::SHELL_SAFE);
77+
$cmd[] = $cleaned;
78+
$this->shell($cmd);
79+
}
80+
else {
81+
$cleaned = $source;
7282
}
73-
$cmd[] = Shell::arg('>', Shell::SHELL_SAFE);
74-
$cmd[] = $cleaned;
75-
$this->shell($cmd);
7683

7784
// Clean up and finalize files.
85+
if (function_exists('drush_get_context')
86+
&& drush_get_context('DRUSH_VERBOSE')) {
87+
drush_print(dt('PDFTK InfoKey: !bool', array(
88+
'!bool' => var_export($infoKey, 1),
89+
)));
90+
drush_print(dt('PDFTK remove_meta: !bool', array(
91+
'!bool' => var_export($remove_meta, 1),
92+
)));
93+
}
7894
if ($remove_meta) {
7995
$temp = $this->getTempFile('pdf');
8096
$fp = fopen($cleaned, 'r');

0 commit comments

Comments
 (0)