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

Fix xml metadata #1534

Open
wants to merge 4 commits into
base: master
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
25 changes: 14 additions & 11 deletions lib/metadata.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { sfix } from "./string"

class PDFMetadata {
constructor() {
this._metadata = `
this._metadata = sfix`
<?xpacket begin="\ufeff" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

`;
}

_closeTags() {
this._metadata = this._metadata.concat(`
</rdf:RDF>
this._metadata = this._metadata.concat(
' </rdf:RDF>\n' +
sfix`
</x:xmpmeta>
<?xpacket end="w"?>
`);
<?xpacket end="w"?>`,
);
}

append(xml, newline=true) {
this._metadata = this._metadata.concat(xml);
append(xml, newline = true) {
this._metadata = this._metadata.concat(xml);
if (newline)
this._metadata = this._metadata.concat('\n');
this._metadata = this._metadata.concat('\n');
}

getXML() { return this._metadata; }
Expand Down
103 changes: 63 additions & 40 deletions lib/mixins/metadata.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,101 @@
import PDFMetadata from "../metadata"
import { sfix } from "../string"

export default {
initMetadata() {
this.metadata = new PDFMetadata();
},

appendXML(xml, newline=true) { this.metadata.append(xml,newline); },
appendXML(xml, tabs = 0, newline = true) {
tabs += 2
xml = xml.split('\n').map(x => ' '.repeat(4).repeat(tabs) + x).join('\n');
this.metadata.append(xml, newline);
},

_addInfo() {
this.appendXML(`
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreateDate>${this.info.CreationDate.toISOString().split('.')[0]+"Z"}</xmp:CreateDate>
<xmp:CreatorTool>${this.info.Creator}</xmp:CreatorTool>
</rdf:Description>
`
this.appendXML(
sfix`
<rdf:Description rdf:about=""
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
xmlns:dc="http://purl.org/dc/elements/1.1/">`,
0,
);
this.appendXML(
sfix`
<xmp:CreateDate>${this.info.CreationDate.toISOString().split('.')[0] + 'Z'}</xmp:CreateDate>`,
1,
);
if (this.info.Creator) {
this.appendXML(
sfix`
<xmp:CreatorTool>${this.info.Creator}</xmp:CreatorTool>`,
1,
);
}

if (this.info.Title || this.info.Author || this.info.Subject) {
this.appendXML(`
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
`);

if (this.info.Title) {
this.appendXML(`
if (this.info.Title) {
this.appendXML(
sfix`
<dc:title>
<rdf:Alt>
<rdf:li xml:lang="x-default">${this.info.Title}</rdf:li>
</rdf:Alt>
</dc:title>
`);
}
</dc:title>`,
1,
);
}

if (this.info.Author) {
this.appendXML(`
if (this.info.Author) {
this.appendXML(
sfix`
<dc:creator>
<rdf:Seq>
<rdf:li>${this.info.Author}</rdf:li>
</rdf:Seq>
</dc:creator>
`);
}
</dc:creator>`,
1,
);
}

if (this.info.Subject) {
this.appendXML(`
if (this.info.Subject) {
this.appendXML(
sfix`
<dc:description>
<rdf:Alt>
<rdf:li xml:lang="x-default">${this.info.Subject}</rdf:li>
</rdf:Alt>
</dc:description>
`);
}

this.appendXML(`
</rdf:Description>
`);
</dc:description>`,
1,
);
}

this.appendXML(`
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Producer>${this.info.Creator}</pdf:Producer>`, false);
if (this.info.Producer) {
this.appendXML(
sfix`
<pdf:Producer>${this.info.Producer}</pdf:Producer>`,
1,
);
}

if (this.info.Keywords) {
this.appendXML(`
<pdf:Keywords>${this.info.Keywords}</pdf:Keywords>`, false);
this.appendXML(
sfix`
<pdf:Keywords>${this.info.Keywords}</pdf:Keywords>`,
1,
);
}

this.appendXML(`
</rdf:Description>
`);
this.appendXML(
sfix`
</rdf:Description>`,
0,
);
},

endMetadata() {
this._addInfo();

this.metadata.end();

/*
Expand Down
12 changes: 6 additions & 6 deletions lib/mixins/pdfa.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { sfix } from "../string"

export default {
initPDFA(pSubset) {
Expand Down Expand Up @@ -42,12 +43,11 @@ export default {
},

_getPdfaid() {
return `
<rdf:Description xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/" rdf:about="">
<pdfaid:part>${this.subset}</pdfaid:part>
<pdfaid:conformance>${this.subset_conformance}</pdfaid:conformance>
</rdf:Description>
`;
return sfix`
<rdf:Description xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/" rdf:about="">
<pdfaid:part>${this.subset}</pdfaid:part>
<pdfaid:conformance>${this.subset_conformance}</pdfaid:conformance>
</rdf:Description>`;
},

_addPdfaMetadata() {
Expand Down
6 changes: 3 additions & 3 deletions lib/mixins/pdfua.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sfix } from "../string"

export default {

Expand All @@ -14,11 +15,10 @@ export default {
},

_getPdfuaid() {
return `
return sfix`
<rdf:Description xmlns:pdfuaid="http://www.aiim.org/pdfua/ns/id/" rdf:about="">
<pdfuaid:part>${this.subset}</pdfuaid:part>
</rdf:Description>
`;
</rdf:Description>`;
},

}
12 changes: 12 additions & 0 deletions lib/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export const sfix = (strings, ...expressions) => {
const indent = !strings[0].startsWith('\n')
? null
: new RegExp(`\n {${strings[0].match(/\n+( *)/)[1].length}}`, 'g');
return expressions
.reduce(
(acc, expr, i) => `${acc}${expr}${strings[i + 1].replace(indent, '\n')}`,
strings[0].replace(indent, '\n'),
)
.replace(/^\n|\n$/g, '');
};