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

feat: add pdfkit subsets mixin #3059

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/witty-mugs-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-pdf/pdfkit": patch
---

feat: add pdfkit subsets mixin
14 changes: 13 additions & 1 deletion packages/pdfkit/src/document.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
PDFDocument - represents an entire PDF document
By Devon Govett
*/

import stream from 'stream';
import PDFObject from './object';
import PDFReference from './reference';
Expand All @@ -15,11 +20,12 @@ import MarkingsMixin from './mixins/markings';
import AcroFormMixin from './mixins/acroform';
import AttachmentsMixin from './mixins/attachments';
import MetadataMixin from './mixins/metadata';
import SubsetMixin from './mixins/subsets';
import capitalize from './utils/capitalize';

class PDFDocument extends stream.Readable {
constructor(options = {}) {
super();
super(options);
this.options = options;

// PDF version
Expand Down Expand Up @@ -93,6 +99,7 @@ class PDFDocument extends stream.Readable {
this.initText();
this.initImages();
this.initOutline();
this.initSubset(options);
// this.initMarkings(options)

// Initialize the metadata
Expand Down Expand Up @@ -267,6 +274,10 @@ class PDFDocument extends stream.Readable {
this.endOutline();
// this.endMarkings();

if (this.subset) {
this.endSubset();
}

this._root.end();
this._root.data.Pages.end();
this._root.data.Names.end();
Expand Down Expand Up @@ -370,5 +381,6 @@ mixin(OutlineMixin);
mixin(MarkingsMixin);
mixin(AcroFormMixin);
mixin(AttachmentsMixin);
mixin(SubsetMixin);

export default PDFDocument;
58 changes: 58 additions & 0 deletions packages/pdfkit/src/mixins/pdfa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'fs';

export default {
initPDFA(pSubset) {
if (pSubset.charAt(pSubset.length - 3) === '-') {
this.subset_conformance = pSubset
.charAt(pSubset.length - 1)
.toUpperCase();
this.subset = parseInt(pSubset.charAt(pSubset.length - 2));
} else {
// Default to Basic conformance when user doesn't specify
this.subset_conformance = 'B';
this.subset = parseInt(pSubset.charAt(pSubset.length - 1));
}
},

endSubset() {
this._addPdfaMetadata();
this._addColorOutputIntent();
},

_addColorOutputIntent() {
const iccProfile = fs.readFileSync(
`${__dirname}/data/sRGB_IEC61966_2_1.icc`
);

const colorProfileRef = this.ref({
Length: iccProfile.length,
N: 3
});
colorProfileRef.write(iccProfile);
colorProfileRef.end();

const intentRef = this.ref({
Type: 'OutputIntent',
S: 'GTS_PDFA1',
Info: new String('sRGB IEC61966-2.1'),
OutputConditionIdentifier: new String('sRGB IEC61966-2.1'),
DestOutputProfile: colorProfileRef
});
intentRef.end();

this._root.data.OutputIntents = [intentRef];
},

_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>
`;
},

_addPdfaMetadata() {
this.appendXML(this._getPdfaid());
}
};
21 changes: 21 additions & 0 deletions packages/pdfkit/src/mixins/pdfua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
initPDFUA() {
this.subset = 1;
},

endSubset() {
this._addPdfuaMetadata();
},

_addPdfuaMetadata() {
this.appendXML(this._getPdfuaid());
},

_getPdfuaid() {
return `
<rdf:Description xmlns:pdfuaid="http://www.aiim.org/pdfua/ns/id/" rdf:about="">
<pdfuaid:part>${this.subset}</pdfuaid:part>
</rdf:Description>
`;
}
};
29 changes: 29 additions & 0 deletions packages/pdfkit/src/mixins/subsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import PDFA from './pdfa';
import PDFUA from './pdfua';

export default {
_importSubset(subset) {
Object.assign(this, subset);
},

initSubset(options) {
switch (options.subset) {
case 'PDF/A-1':
case 'PDF/A-1a':
case 'PDF/A-1b':
case 'PDF/A-2':
case 'PDF/A-2a':
case 'PDF/A-2b':
case 'PDF/A-3':
case 'PDF/A-3a':
case 'PDF/A-3b':
this._importSubset(PDFA);
this.initPDFA(options.subset);
break;
case 'PDF/UA':
this._importSubset(PDFUA);
this.initPDFUA();
break;
}
}
};