| 1 |
#!/usr/bin/env perl |
| 2 |
use warnings; |
| 3 |
|
| 4 |
############################################################# |
| 5 |
############################################################# |
| 6 |
|
| 7 |
######################################################################## |
| 8 |
# hyperxmp-add-bytecount # |
| 9 |
# Adds/updates byteCount specification in XMP packet in pdf file(s) # |
| 10 |
# made by hyperxmp, with byteCount = file size. # |
| 11 |
# Copyright (C) 2020-2022 John Collins <[email protected]> # |
| 12 |
# and Scott Pakin, <[email protected]> # |
| 13 |
# # |
| 14 |
# This program may be distributed and/or modified under the conditions # |
| 15 |
# of the LaTeX Project Public License, either version 1.3c of this # |
| 16 |
# license or (at your option) any later version. # |
| 17 |
# # |
| 18 |
# The latest version of this license is in: # |
| 19 |
# # |
| 20 |
# http://www.latex-project.org/lppl.txt # |
| 21 |
# # |
| 22 |
# and version 1.3c or later is part of all distributions of LaTeX # |
| 23 |
# version 2008/05/04 or later. # |
| 24 |
######################################################################## |
| 25 |
|
| 26 |
$name = 'hyperref-add-bytecount'; |
| 27 |
$version = '1.1-2020-11-20'; |
| 28 |
$maintainer |
| 29 |
= 'John Collins, [email protected]; Scott Pakin, [email protected]'; |
| 30 |
|
| 31 |
my $exit_code = 0; |
| 32 |
|
| 33 |
if ( (! @ARGV) || ($ARGV[0] =~ /^(-|--)(h|help)$/) ) { |
| 34 |
print "$name $version.\n", |
| 35 |
"Usage: $name [options] pdf_filename(s)\n", |
| 36 |
" Adds/updates byteCount specification in XMP packet in pdf file(s) from\n", |
| 37 |
" hyperxmp, with byteCount = file size.\n", |
| 38 |
" No change if there's no XMP packet of the form produced by hyperxmp.\n", |
| 39 |
"Options:\n", |
| 40 |
" -help or -h Output usage information.\n", |
| 41 |
" -version or -v Output version information.\n", |
| 42 |
"Bug reports to:\n $maintainer.\n"; |
| 43 |
exit; |
| 44 |
} elsif ( $ARGV[0] =~ /^(-|--)(v|version)$/ ) { |
| 45 |
print "$name $version.\n", |
| 46 |
"Bug reports to:\n $maintainer.\n"; |
| 47 |
exit; |
| 48 |
} |
| 49 |
|
| 50 |
foreach (@ARGV) { |
| 51 |
if ( ! fix_pdf($_) ) { $exit_code = 1; } |
| 52 |
} |
| 53 |
|
| 54 |
exit $exit_code; |
| 55 |
|
| 56 |
#====================================================== |
| 57 |
|
| 58 |
sub fix_pdf { |
| 59 |
# Change/insert byteCount field with correct file length, while preserving |
| 60 |
# the file size and the length of the stream containing xmp metadata. |
| 61 |
# Return 1 on success, else 0. |
| 62 |
|
| 63 |
local $pdf_name = shift; |
| 64 |
local $tmp_name = "$pdf_name.new.pdf"; |
| 65 |
local $pdf_size = (stat($pdf_name))[7]; |
| 66 |
warn "Inserting/correcting byteCount field in '$pdf_name' ...\n"; |
| 67 |
|
| 68 |
# Strings surrounding (and identifying) the byteCount field, and other |
| 69 |
# parts of the xmp packet: |
| 70 |
local $xmp_start = '<x:xmpmeta xmlns:x="adobe:ns:meta/">'; |
| 71 |
local $decl_bC = '<pdfaProperty:name>byteCount</pdfaProperty:name>'; |
| 72 |
local $pre_bC = '<prism:byteCount>'; |
| 73 |
local $post_bC = '</prism:byteCount>'; |
| 74 |
local $pC = '<prism:pageCount>'; |
| 75 |
local $rd_end = '</rdf:Description>'; |
| 76 |
local $xmp_end = '</x:xmpmeta>'; |
| 77 |
|
| 78 |
local *PDF; |
| 79 |
local *TMP; |
| 80 |
|
| 81 |
if (! open PDF, "<", $pdf_name ) { |
| 82 |
warn " Cannot read '$pdf_name'\n"; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
if ( ! open TMP, ">", $tmp_name ) { |
| 86 |
warn " Cannot write temporary file '$tmp_name'\n"; |
| 87 |
close PDF; |
| 88 |
return 0; |
| 89 |
} |
| 90 |
local $status = 0; # 0 = no XMP packet, 1 = success, >= errors |
| 91 |
while ( <PDF> ) { |
| 92 |
# Only examine first XMP packet: |
| 93 |
if ( ($status == 0) && /^\s*\Q$xmp_start\E/ ) { |
| 94 |
local @xmp = $_; |
| 95 |
local $len_padding = 0; |
| 96 |
local $xmp_after_line = ''; |
| 97 |
&xmp_get_mod; |
| 98 |
print TMP @xmp; |
| 99 |
# Insert correct padding to leave file size unchanged: |
| 100 |
while ( $len_padding > 0 ) { |
| 101 |
my $len_line = 64; |
| 102 |
if ( $len_line > $len_padding ) { $len_line = $len_padding; } |
| 103 |
$len_padding -= $len_line; |
| 104 |
print TMP (' ' x ($len_line - 1) ), "\n"; |
| 105 |
} |
| 106 |
print TMP $xmp_after_line; |
| 107 |
$xmp_after_line = ''; |
| 108 |
} |
| 109 |
else { |
| 110 |
print TMP "$_"; |
| 111 |
} |
| 112 |
} |
| 113 |
close PDF; |
| 114 |
close TMP; |
| 115 |
|
| 116 |
if ($status == 0) { |
| 117 |
warn " Could not insert/modify byteCount, since no XMP packet was found.\n"; |
| 118 |
warn " So '$pdf_name' is unchanged,\n", |
| 119 |
" and I will delete temporary file '$tmp_name'.\n"; |
| 120 |
unlink $tmp_name; |
| 121 |
} elsif ($status == 1) { |
| 122 |
rename $tmp_name, $pdf_name |
| 123 |
or die " Cannot move temporary file '$tmp_name' to '$pdf_name'.\n", |
| 124 |
" Error is '$!'\n"; |
| 125 |
} else { |
| 126 |
warn " Could not insert correct byteCount. See above for reason.\n"; |
| 127 |
warn " So '$pdf_name' is unchanged,\n", |
| 128 |
" and I will delete temporary file '$tmp_name'.\n"; |
| 129 |
unlink $tmp_name; |
| 130 |
} |
| 131 |
return ($status == 1); |
| 132 |
} |
| 133 |
|
| 134 |
#====================================================== |
| 135 |
|
| 136 |
sub xmp_get_mod { |
| 137 |
# Get xmp packet, given that @xmp contains its first line. |
| 138 |
# Get amount of trailing padding, and line after that. |
| 139 |
# If possible, insert a byteCount field: |
| 140 |
# Either replace existing specification, if it exists, |
| 141 |
# or insert one in expected place for hyperxmp, if the XMP packet |
| 142 |
# matches what hyperxmp would produce. |
| 143 |
# Return xmp packet in @xmp, amount of padding needed in $len_padding, |
| 144 |
# line after that in $xmp_after_line, and error code in $error. |
| 145 |
# Set $status appropriately: 1 for success; >=1 for failure. |
| 146 |
|
| 147 |
$len_padding = 0; |
| 148 |
$xmp_after_line = ''; |
| 149 |
|
| 150 |
my $bC_index = -1; |
| 151 |
my $xmp_end_found = 0; |
| 152 |
my $decl_bC_found = 0; |
| 153 |
while ( <PDF> ) { |
| 154 |
push @xmp, $_; |
| 155 |
if ( /^\s*\Q$xmp_end\E/ ) { |
| 156 |
$xmp_end_found = 1; |
| 157 |
# Get amount of padding; |
| 158 |
while (<PDF>) { |
| 159 |
if ( /^\s*$/ ) { |
| 160 |
$len_padding += length($_); |
| 161 |
} else { |
| 162 |
$xmp_after_line = $_; |
| 163 |
last; |
| 164 |
} |
| 165 |
} |
| 166 |
last; |
| 167 |
} |
| 168 |
elsif ( $bC_index >= 0 ){ |
| 169 |
next; |
| 170 |
} |
| 171 |
# Rest of conditions only apply if no place yet found for byteCount |
| 172 |
# specification. |
| 173 |
elsif ( /^(\s*)\Q$pre_bC\E.*?\Q$post_bC\E\s*$/ ) { |
| 174 |
$bC_index = $#xmp; |
| 175 |
} |
| 176 |
elsif ( /^\s*\Q$decl_bC\E/ ) { |
| 177 |
$decl_bC_found = 1; |
| 178 |
} |
| 179 |
elsif ( /^(\s*)\Q$rd_end\E/ ){ |
| 180 |
# End of rdf:Description block. |
| 181 |
# So having previous declaration of byteCount is irrelevant. |
| 182 |
$decl_bC_found = 0; |
| 183 |
} |
| 184 |
elsif ( $decl_bC_found && /^(\s*)\Q$pC\E/ ){ |
| 185 |
$bC_index = $#xmp; |
| 186 |
pop @xmp; |
| 187 |
push @xmp, '', $_; |
| 188 |
} |
| 189 |
|
| 190 |
} # End reading of XMP |
| 191 |
|
| 192 |
if ($bC_index < 0) { |
| 193 |
if ( ! $xmp_end_found ) { |
| 194 |
warn " End of XMP packet not found.\n"; |
| 195 |
$status = 2; |
| 196 |
} |
| 197 |
elsif ( ! $decl_bC_found ) { |
| 198 |
warn " XMP packet not in appropriate hyperxmp-compatible format.\n"; |
| 199 |
$status = 3; |
| 200 |
} |
| 201 |
return; |
| 202 |
} |
| 203 |
my $new_line = ' ' . $pre_bC . $pdf_size . $post_bC . "\n"; |
| 204 |
my $old_line = $xmp[$bC_index]; |
| 205 |
my $delta_len = length($new_line) - length($old_line); |
| 206 |
if ($delta_len > $len_padding) { |
| 207 |
warn " Cannot get padding correct for '$pdf_name'.\n", |
| 208 |
" Length change of bC line = $delta_len; ", |
| 209 |
" Padding bytes available = $len_padding.\n"; |
| 210 |
$status = 4; |
| 211 |
return; |
| 212 |
} else { |
| 213 |
$len_padding -= $delta_len; |
| 214 |
$xmp[$bC_index] = $new_line; |
| 215 |
$status = 1; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
#====================================================== |