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

Add the ability to copy a pdf #986

Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions src/api/PDFDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export default class PDFDocument {
throwOnInvalidObject = false,
updateMetadata = true,
capNumbers = false,
returnCopy = false,
} = options;

assertIs(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
assertIs(ignoreEncryption, 'ignoreEncryption', ['boolean']);
assertIs(parseSpeed, 'parseSpeed', ['number']);
assertIs(throwOnInvalidObject, 'throwOnInvalidObject', ['boolean']);
assertIs(returnCopy, 'returnCopy', ['boolean']);

const bytes = toUint8Array(pdf);
const context = await PDFParser.forBytesWithOptions(
Expand All @@ -147,6 +149,42 @@ export default class PDFDocument {
throwOnInvalidObject,
capNumbers,
).parseDocument();

if (returnCopy) {
const pdfDoc = new PDFDocument(context, ignoreEncryption, updateMetadata);
const pdfCopy = await PDFDocument.create();
const contentPages = await pdfCopy.copyPages(
pdfDoc,
pdfDoc.getPageIndices(),
);

for (const page of contentPages) {
pdfCopy.addPage(page);
}
if (pdfDoc.getAuthor() !== undefined) {
pdfCopy.setAuthor(pdfDoc.getAuthor()!);
}
if (pdfDoc.getCreationDate() !== undefined) {
pdfCopy.setCreationDate(pdfDoc.getCreationDate()!);
}
if (pdfDoc.getCreator() !== undefined) {
pdfCopy.setCreator(pdfDoc.getCreator()!);
}
if (pdfDoc.getModificationDate() !== undefined) {
pdfCopy.setModificationDate(pdfDoc.getModificationDate()!);
}
if (pdfDoc.getProducer() !== undefined) {
pdfCopy.setProducer(pdfDoc.getProducer()!);
}
if (pdfDoc.getSubject() !== undefined) {
pdfCopy.setSubject(pdfDoc.getSubject()!);
}
if (pdfDoc.getTitle() !== undefined) pdfCopy.setTitle(pdfDoc.getTitle()!);
pdfCopy.defaultWordBreaks = pdfDoc.defaultWordBreaks;

return pdfCopy;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are related to, but still distinct from, loading a document. So I think this would fit better as a .copy() method on PDFDocument. Then, instead of:

PDFDocument.load(..., { returnCopy: true })

we could do:

PDFDocument.load(...).copy()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you've done this, be sure to add a proper doc comment to the .copy() method


return new PDFDocument(context, ignoreEncryption, updateMetadata);
}

Expand Down
1 change: 1 addition & 0 deletions src/api/PDFDocumentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface LoadOptions {
throwOnInvalidObject?: boolean;
updateMetadata?: boolean;
capNumbers?: boolean;
returnCopy?: boolean;
}

export interface CreateOptions {
Expand Down