diff --git a/lib/metadata.js b/lib/metadata.js index 0742bd50..1b8736e7 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -1,25 +1,28 @@ +import { sfix } from "./string" class PDFMetadata { constructor() { - this._metadata = ` + this._metadata = sfix` - - + + + `; } - + _closeTags() { - this._metadata = this._metadata.concat(` - + this._metadata = this._metadata.concat( + ' \n' + + sfix` - - `); + `, + ); } - 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; } diff --git a/lib/mixins/metadata.js b/lib/mixins/metadata.js index 9268da62..ede9c7be 100644 --- a/lib/mixins/metadata.js +++ b/lib/mixins/metadata.js @@ -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(` - - ${this.info.CreationDate.toISOString().split('.')[0]+"Z"} - ${this.info.Creator} - - ` + this.appendXML( + sfix` + `, + 0, + ); + this.appendXML( + sfix` + ${this.info.CreationDate.toISOString().split('.')[0] + 'Z'}`, + 1, ); + if (this.info.Creator) { + this.appendXML( + sfix` + ${this.info.Creator}`, + 1, + ); + } - if (this.info.Title || this.info.Author || this.info.Subject) { - this.appendXML(` - - `); - - if (this.info.Title) { - this.appendXML(` + if (this.info.Title) { + this.appendXML( + sfix` ${this.info.Title} - - `); - } + `, + 1, + ); + } - if (this.info.Author) { - this.appendXML(` + if (this.info.Author) { + this.appendXML( + sfix` ${this.info.Author} - - `); - } + `, + 1, + ); + } - if (this.info.Subject) { - this.appendXML(` + if (this.info.Subject) { + this.appendXML( + sfix` ${this.info.Subject} - - `); - } - - this.appendXML(` - - `); + `, + 1, + ); } - this.appendXML(` - - ${this.info.Creator}`, false); + if (this.info.Producer) { + this.appendXML( + sfix` + ${this.info.Producer}`, + 1, + ); + } if (this.info.Keywords) { - this.appendXML(` - ${this.info.Keywords}`, false); + this.appendXML( + sfix` + ${this.info.Keywords}`, + 1, + ); } - this.appendXML(` - - `); + this.appendXML( + sfix` + `, + 0, + ); }, endMetadata() { this._addInfo(); - + this.metadata.end(); /* diff --git a/lib/mixins/pdfa.js b/lib/mixins/pdfa.js index f06dfc4a..7d9aea40 100644 --- a/lib/mixins/pdfa.js +++ b/lib/mixins/pdfa.js @@ -1,4 +1,5 @@ import fs from 'fs'; +import { sfix } from "../string" export default { initPDFA(pSubset) { @@ -42,12 +43,11 @@ export default { }, _getPdfaid() { - return ` - - ${this.subset} - ${this.subset_conformance} - - `; + return sfix` + + ${this.subset} + ${this.subset_conformance} + `; }, _addPdfaMetadata() { diff --git a/lib/mixins/pdfua.js b/lib/mixins/pdfua.js index 89fd9e3a..f86009b4 100644 --- a/lib/mixins/pdfua.js +++ b/lib/mixins/pdfua.js @@ -1,3 +1,4 @@ +import { sfix } from "../string" export default { @@ -14,11 +15,10 @@ export default { }, _getPdfuaid() { - return ` + return sfix` ${this.subset} - - `; + `; }, } \ No newline at end of file diff --git a/lib/string.js b/lib/string.js new file mode 100644 index 00000000..d60cb9db --- /dev/null +++ b/lib/string.js @@ -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, ''); +}; \ No newline at end of file