Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const PDFLib = require("pdf-lib");
const EventEmitter = require('events');
const PDFDocumentWriter = require('./writer');
class PostProcesser extends EventEmitter {
constructor(pdf) {
super();
if (!pdf) {
throw "Must pass a PDF Buffer to PostProcesser"
}
this.pdf = pdf
this.pdfDoc = PDFLib.PDFDocumentFactory.load(pdf);
}
metadata(meta) {
if (meta.keywords && typeof meta.keywords === "string") {
meta.keywords = meta.keywords.split(",");
}
if (!meta.keywords) {
meta.keywords = [];
}
// Overwrite Dates
if (!(meta.creationDate instanceof Date)) {
meta.creationDate = new Date();
}
meta.modDate = new Date();
meta.metadataDate = new Date();
// Get the existing Info
let info = this.getInfoDict();
if (!meta.creator) {
meta.creator = info.creator + " + Paged.js";
}
if (!meta.producer) {
meta.producer = info.producer;
}
// Add meta
this.addXmpMetadata(meta);
this.updateInfoDict(meta);
}
getInfoDict(){
// Info Reference in Skia pdfs is always 1st
let ref = PDFLib.PDFIndirectReference.forNumbers(1, 0);
let info = this.pdfDoc.index.lookup(ref);
return {
title: info.getMaybe("Title") && info.getMaybe("Title").string,
subject: info.getMaybe("Subject") && info.getMaybe("Subject").string,
keywords: info.getMaybe("Keywords") && info.getMaybe("Keywords").string,
author: info.getMaybe("Author") && info.getMaybe("Author").string,
creationDate: info.getMaybe("CreationDate") && info.getMaybe("CreationDate").string,
modDate: info.getMaybe("ModDate") && info.getMaybe("ModDate").string,
creator: info.getMaybe("Creator") && info.getMaybe("Creator").string,
producer: info.getMaybe("Producer") && info.getMaybe("Producer").string
}
}
updateInfoDict(meta) {
// Info Reference in Skia pdfs is always 1st
let ref = PDFLib.PDFIndirectReference.forNumbers(1, 0);
let info = this.pdfDoc.index.lookup(ref);
if (meta.title) {
info.set("Title", PDFLib.PDFString.fromString(meta.title));
}
if (meta.subject) {
info.set("Subject", PDFLib.PDFString.fromString(meta.subject));
}
if (meta.keywords && meta.keywords.length) {
info.set("Keywords", PDFLib.PDFString.fromString(meta.keywords.join(", ")));
}
if (meta.author) {
info.set("Author", PDFLib.PDFString.fromString(meta.author));
}
if (meta.creationDate) {
info.set("CreationDate", PDFLib.PDFString.fromString(meta.creationDate.toISOString()));
}
if (meta.modDate) {
info.set("ModDate", PDFLib.PDFString.fromString(meta.modDate.toISOString()));
}
if (meta.creator) {
info.set("Creator", PDFLib.PDFString.fromString(meta.creator));
}
if (meta.producer) {
info.set("Producer", PDFLib.PDFString.fromString(meta.producer));
}
}
addXmpMetadata(meta) {
const charCodes = (str) => str.split('').map((c) => c.charCodeAt(0));
const typedArrayFor = (str) => new Uint8Array(charCodes(str));
const whitespacePadding = new Array(20).fill(' '.repeat(100)).join('\n');
const metadataXML = `
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.2-c001 63.139439, 2010/09/27-13:37:26">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:format>application/pdf</dc:format>
<dc:creator>
<rdf:Seq>
<rdf:li>${meta.author}</rdf:li>
</rdf:Seq>
</dc:creator>
<dc:title>
<rdf:Alt>
<rdf:li xml:lang="x-default">${meta.title}</rdf:li>
</rdf:Alt>
</dc:title>
<dc:subject>
<rdf:Bag>
${meta.keywords
.map((keyword) => `<rdf:li>${keyword}</rdf:li>`)
.join('\n')}
</rdf:Bag>
</dc:subject>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreatorTool>${meta.creatorTool}</xmp:CreatorTool>
<xmp:CreateDate>${meta.creationDate.toISOString()}</xmp:CreateDate>
<xmp:ModifyDate>${meta.modDate.toISOString()}</xmp:ModifyDate>
<xmp:MetadataDate>${meta.metadataDate.toISOString()}</xmp:MetadataDate>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Subject>${meta.subject}</pdf:Subject>
<pdf:Producer>${meta.producer}</pdf:Producer>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
${whitespacePadding}
<?xpacket end="w"?>
`.trim();
const metadataStreamDict = PDFLib.PDFDictionary.from(
{
Type: PDFLib.PDFName.from('Metadata'),
Subtype: PDFLib.PDFName.from('XML'),
Length: PDFLib.PDFNumber.fromNumber(metadataXML.length),
},
this.pdfDoc.index,
);
const metadataStream = PDFLib.PDFRawStream.from(
metadataStreamDict,
typedArrayFor(metadataXML),
);
const metadataStreamRef = this.pdfDoc.register(metadataStream);
this.pdfDoc.catalog.set('Metadata', metadataStreamRef);
};
boxes(pages) {
const pdfPages = this.pdfDoc.getPages();
pdfPages.forEach((pdfPage, index) => {
const page = pages[index];
if (!page) {
return; // page was not rendered
}
let { boxes } = page;
if (Object.is(boxes.media, boxes.crop)) {
return; // No bleed set
}
const rectangle = PDFLib.PDFArray.fromArray(
[
PDFLib.PDFNumber.fromNumber(boxes.crop.x * 2),
PDFLib.PDFNumber.fromNumber(boxes.crop.y * 2),
PDFLib.PDFNumber.fromNumber(boxes.crop.width),
PDFLib.PDFNumber.fromNumber(boxes.crop.height),
],
pdfPage.index,
);
// pdfPage.set("ArtBox", rectangle);
pdfPage.set("TrimBox", rectangle);
pdfPage.set("CropBox", rectangle);
});
}
updatePageBoxes(page) {
console.log(page);
}
save() {
let writer = new PDFDocumentWriter();
const pdfBytes = writer.saveToBytesWithXRefTable(this.pdfDoc);
this.pdf = pdfBytes;
return this.pdf;
}
}
module.exports = PostProcesser;