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 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
48 changes: 48 additions & 0 deletions src/api/PDFDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,54 @@ export default class PDFDocument {
return copiedPages;
}

/**
* Get a copy of this document.
* > **NOTE:** This method won't copy all information over to the new doc (acroforms, etc...).
*
* For example:
* ```js
* const srcDoc = await PDFDocument.load(...)
* const pdfDoc = await srcDoc.copy()
* ```
* @returns Resolves with a copy this document.
*/
async copy(): Promise<PDFDocument> {
const pdfCopy = await PDFDocument.create();
const contentPages = await pdfCopy.copyPages(
this,
this.getPageIndices(),
);

for (const page of contentPages) {
pdfCopy.addPage(page);
}

if (this.getAuthor() !== undefined) {
pdfCopy.setAuthor(this.getAuthor()!);
}
if (this.getCreationDate() !== undefined) {
pdfCopy.setCreationDate(this.getCreationDate()!);
}
if (this.getCreator() !== undefined) {
pdfCopy.setCreator(this.getCreator()!);
}
if (this.getModificationDate() !== undefined) {
pdfCopy.setModificationDate(this.getModificationDate()!);
}
if (this.getProducer() !== undefined) {
pdfCopy.setProducer(this.getProducer()!);
}
if (this.getSubject() !== undefined) {
pdfCopy.setSubject(this.getSubject()!);
}
if (this.getTitle() !== undefined) {
pdfCopy.setTitle(this.getTitle()!);
}
pdfCopy.defaultWordBreaks = this.defaultWordBreaks;

return pdfCopy;
}

/**
* Add JavaScript to this document. The supplied `script` is executed when the
* document is opened. The `script` can be used to perform some operation
Expand Down
44 changes: 44 additions & 0 deletions tests/api/PDFDocument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,47 @@ describe(`PDFDocument`, () => {
});
});
});

describe(`copy() method`, () => {
let pdfDoc: PDFDocument;
let srcDoc: PDFDocument;
beforeAll(async () => {
const parseSpeed = ParseSpeeds.Fastest;
srcDoc = await PDFDocument.load(unencryptedPdfBytes, { parseSpeed });
const title = '🥚 The Life of an Egg 🍳';
const author = 'Humpty Dumpty';
const subject = '📘 An Epic Tale of Woe 📖';
const keywords = ['eggs', 'wall', 'fall', 'king', 'horses', 'men', '🥚'];
const producer = 'PDF App 9000 🤖';
const creator = 'PDF App 8000 🤖';

// Milliseconds will not get saved, so these dates do not have milliseconds.
const creationDate = new Date('1997-08-15T01:58:37Z');
const modificationDate = new Date('2018-12-21T07:00:11Z');

srcDoc.setTitle(title);
srcDoc.setAuthor(author);
srcDoc.setSubject(subject);
srcDoc.setKeywords(keywords);
srcDoc.setProducer(producer);
srcDoc.setCreator(creator);
srcDoc.setCreationDate(creationDate);
srcDoc.setModificationDate(modificationDate);
pdfDoc = await srcDoc.copy();
});

it(`Returns a pdf with the same number of pages`, async () => {
expect(pdfDoc.getPageCount()).toBe(srcDoc.getPageCount());
});

it(`Can copy author, creationDate, creator, producer, subject, title, defaultWordBreaks`, async () => {
expect(pdfDoc.getAuthor()).toBe(srcDoc.getAuthor());
expect(pdfDoc.getCreationDate()).toStrictEqual(srcDoc.getCreationDate());
expect(pdfDoc.getCreator()).toBe(srcDoc.getCreator());
expect(pdfDoc.getModificationDate()).toStrictEqual(srcDoc.getModificationDate());
expect(pdfDoc.getProducer()).toBe(srcDoc.getProducer());
expect(pdfDoc.getSubject()).toBe(srcDoc.getSubject());
expect(pdfDoc.getTitle()).toBe(srcDoc.getTitle());
expect(pdfDoc.defaultWordBreaks).toEqual(srcDoc.defaultWordBreaks);
});
});