Debian Codegraph @

src/setpdfmetadata

raw
1 #! /usr/bin/perl -w
2 #
3 # PDF Metadata Updater
4 # Copyright (C) 2009-2024 by Thomas Dreibholz
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Contact: [email protected]
20 #
21
22 use strict;
23 use POSIX;
24 use Encode;
25 use PDF::API2;
26 use subs qw(updatePDFMetadataRecursion);
27 use subs qw(updatePDFMetadata);
28
29
30 # ###### Get next command line ##############################################
31 sub getNextCommand($) {
32 my $inputFile = shift; # Metadata input file
33 my $command;
34
35 while ($command = <$inputFile>) {
36 chomp $command;
37 # ====== Skip empty lines =============================================
38 next if $command =~ /^$/;
39 # ====== Skip comments (#, //) ========================================
40 next if $command =~ /^\/\//;
41 next if $command =~ /^#/;
42 last;
43 }
44 # if($command) {
45 # printf("INPUT: %s\n",$command);
46 # }
47 return $command;
48 }
49
50
51 # ###### Recursion for updating PDF metadata (outlines, properties) #########
52 sub updatePDFMetadataRecursion($$$$) {
53 my $pdfFile = shift; # PDF file
54 my $parentOutline = shift; # Parent outline in PDF's outline tree
55 my $command = shift; # Command
56 my $inputFile = shift; # Metadata input file
57
58 my $currentLevel = -12345;
59 my $currentOutline;
60
61 commandHandlingLoop: while ($command) {
62 # ===== Handle "outline" command ======================================
63 if($command =~ /^outline/) {
64 my ($dummy, $newLevel, $page, $bookmark) = split(/ /, $command, 4);
65 if($currentLevel < 0) {
66 $currentLevel= $newLevel;
67 }
68
69 # ====== New subtree in outline hierarchy ==========================
70 if ($newLevel > $currentLevel) {
71 $command = updatePDFMetadataRecursion($pdfFile, $currentOutline,
72 $command, $inputFile);
73 next commandHandlingLoop; # Handle returned command by loop
74 }
75 # ====== Go up in outline hierarchy ================================
76 elsif ($newLevel < $currentLevel) {
77 # Return $command in updatePDFMetadataRecursion() call above;
78 # this will lead to the creation of a new item.
79 return($command);
80 }
81 # ====== Add item in outline hierarchy =============================
82 else {
83 Encode::from_to($bookmark, "utf8", "iso-8859-1");
84 $currentOutline = $parentOutline->outline;
85 $currentOutline->title($bookmark);
86 my $pdfPage = $pdfFile->{pagestack}->[$page - 1];
87 $currentOutline->dest($pdfPage);
88 }
89 }
90
91 # ===== Handle "title" command ========================================
92 if($command =~ /^title/) {
93 my ($dummy, $title) = split(/ /, $command, 2);
94 Encode::from_to($title, "utf8", "iso-8859-1");
95 $pdfFile->info('Title' => $title);
96 }
97
98 # ===== Handle "subject" command ======================================
99 if($command =~ /^subject/) {
100 my ($dummy, $subject) = split(/ /, $command, 2);
101 Encode::from_to($subject, "utf8", "iso-8859-1");
102 $pdfFile->info('Subject' => $subject)
103 }
104
105 # ===== Handle "author" command =======================================
106 if($command =~ /^author/) {
107 my ($dummy, $author) = split(/ /, $command, 2);
108 Encode::from_to($author, "utf8", "iso-8859-1");
109 $pdfFile->info('Author' => $author)
110 }
111
112 # ===== Handle "creator" command ======================================
113 if($command =~ /^creator/) {
114 my ($dummy, $creator) = split(/ /, $command, 2);
115 Encode::from_to($creator, "utf8", "iso-8859-1");
116 $pdfFile->info('Creator' => $creator)
117 }
118
119 # ===== Handle "producer" command =====================================
120 if($command =~ /^producer/) {
121 my ($dummy, $producer) = split(/ /, $command, 2);
122 Encode::from_to($producer, "utf8", "iso-8859-1");
123 $pdfFile->info('Producer' => $producer)
124 }
125
126 # ===== Handle "keywords" command =====================================
127 if($command =~ /^keywords/) {
128 my ($dummy, $keywords) = split(/ /, $command, 2);
129 Encode::from_to($keywords, "utf8", "iso-8859-1");
130 $pdfFile->info('Keywords' => $keywords)
131 }
132
133 $command = getNextCommand($inputFile);
134 }
135 }
136
137
138 # ###### Update PDF metadata (outlines, properties) #########################
139 sub updatePDFMetadata($$) {
140 my $pdfFile = shift; # PDF file
141 my $metadataFile = shift; # Metadata input file
142
143 my $command = getNextCommand(*metadataFile);
144
145 updatePDFMetadataRecursion($pdfFile, $pdfFile->outlines, $command, $metadataFile);
146 }
147
148
149
150 # ###### Main program #######################################################
151 if ($#ARGV != 2) {
152 printf("Usage: %s input.pdf metadata.meta output.pdf\n", $0);
153 exit 1;
154 }
155
156 # ====== Handle arguments ===================================================
157 my $inputPDFName = $ARGV[0];
158 my $inputMetadataName = $ARGV[1];
159 my $outputPDFName = $ARGV[2];
160 my $pdfFile = PDF::API2->open($inputPDFName);
161 open(metadataFile, "<", $inputMetadataName) or
162 die("ERROR: Unable to open metadata file \"", $inputMetadataName, "\"");
163
164 # ====== Set default author and producer ====================================
165 my $userID=POSIX::getuid();
166 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($userID);
167 if($gcos) {
168 my ($fullName) = split(/,/, $gcos, 2);
169 $pdfFile->info('Author' => $fullName,
170 'Producer' => $fullName);
171 }
172
173 # ====== Set modification time ==============================================
174 my $time = strftime("D:%Y%m%d%H%M%S%z", localtime());
175 $pdfFile->info('ModDate' => $time);
176
177 # ====== Apply metadata changes from metadata file ==========================
178 updatePDFMetadata($pdfFile, *metadataFile);
179
180 # ====== Save results =======================================================
181 $pdfFile->saveas($outputPDFName);
182
183 exit 0;