-
Notifications
You must be signed in to change notification settings - Fork 33
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 getAttachments to PDFDocument #80
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ import { | |
PDFCatalog, | ||
PDFContext, | ||
PDFDict, | ||
PDFArray, | ||
decodePDFRawStream, | ||
PDFStream, | ||
PDFRawStream, | ||
PDFHexString, | ||
PDFName, | ||
PDFObjectCopier, | ||
|
@@ -940,6 +944,47 @@ export default class PDFDocument { | |
this.embeddedFiles.push(embeddedFile); | ||
} | ||
|
||
private getRawAttachments() { | ||
if (!this.catalog.has(PDFName.of('Names'))) return []; | ||
const Names = this.catalog.lookup(PDFName.of('Names'), PDFDict); | ||
|
||
if (!Names.has(PDFName.of('EmbeddedFiles'))) return []; | ||
const EmbeddedFiles = Names.lookup(PDFName.of('EmbeddedFiles'), PDFDict); | ||
|
||
if (!EmbeddedFiles.has(PDFName.of('Names'))) return []; | ||
const EFNames = EmbeddedFiles.lookup(PDFName.of('Names'), PDFArray); | ||
|
||
const rawAttachments = []; | ||
for (let idx = 0, len = EFNames.size(); idx < len; idx += 2) { | ||
const fileName = EFNames.lookup(idx) as PDFHexString | PDFString; | ||
const fileSpec = EFNames.lookup(idx + 1, PDFDict); | ||
rawAttachments.push({ fileName, fileSpec }); | ||
} | ||
|
||
return rawAttachments; | ||
} | ||
|
||
/** | ||
* Get all attachments that are embedded in this document. | ||
* | ||
* > **NOTE:** If you build a document with this library, this won't return | ||
* > anything until you call [[save]] on the document. | ||
* | ||
* @returns Array of attachments with name and data | ||
*/ | ||
getAttachments() { | ||
const rawAttachments = this.getRawAttachments(); | ||
return rawAttachments.map(({ fileName, fileSpec }) => { | ||
const stream = fileSpec | ||
.lookup(PDFName.of('EF'), PDFDict) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we make sure "EF" and "F" key exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. These |
||
.lookup(PDFName.of('F'), PDFStream) as PDFRawStream; | ||
return { | ||
name: fileName.decodeText(), | ||
data: decodePDFRawStream(stream).decode(), | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Embed a font into this document. The input data can be provided in multiple | ||
* formats: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no reason why not? This might be confusing for users...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are the attachments being stored before they are serialized? We can probably include that location when returning the attachments, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that would be better. It's been a while since I've looked at this code, but I vaguely remember that being difficult. I see a comment on
PDFEmbeddedFile.embed
that says something to the effect of it not being embedded untilsave
is called. I also don't see how to get this information out ofPDFEmbeddedFile
.I won't be able to spend a lot of time on this soon, but I'm planning to come back at some point and give it another shot to see if what you're saying is possible.