Skip to content

Embed files associated with the document as a whole - #5221

Merged
laurmaedje merged 72 commits into
typst:mainfrom
NiklasEi:file-embedding
Jan 8, 2025
Merged

laurmaedje merged 72 commits into
typst:mainfrom
NiklasEi:file-embedding

Conversation

@NiklasEi

@NiklasEi NiklasEi commented Oct 16, 2024 •

Copy link
Copy Markdown
Contributor

Resolves #4411
Part of #2499

This PR implements file embeddings associated with the document as a whole. The attachments do not have visible annotations. It also adds an option for PDF/A-3 to allow arbitrary file embeddings with PDF/A compliance. I will gladly move this to a second PR if preferred.
The embeddings are part of a new pdf module:

This will embed the file without a description
#pdf.embed("text.txt")

We can also optionally set description, mime type and relationship of the embedding
#pdf.embed("text.txt", description: "An embedded file", mime-type: "text/plain", relationship: "supplement")

Or embed any bytes into the PDF as a file.
#let some_bytes = read("test.txt")

With just a file name...
#pdf.embed.decode("test.txt", some_bytes)

or all the other additional information
#pdf.embed.decode("a_file_name.txt", some_bytes, description: "A description", mime-type: "text/plain", relationship: "supplement")

Comment thread crates/typst-pdf/src/catalog.rs Resolved
Comment thread crates/typst-pdf/src/embed.rs Resolved
Comment thread crates/typst/src/embed/mod.rs Resolved
Comment thread crates/typst-pdf/src/embed.rs Resolved
@NiklasEi
NiklasEi marked this pull request as ready for review October 17, 2024 17:05
@sirchnik

Copy link
Copy Markdown

#embed is causing pagebreaks for me when using before #set page("a4") or after #pagebreak(weak: true).

Reproduction
#embed.decode("bill-reports.pdf", "test.txt")
#set page("a4")
= header
#pagebreak(weak: true)
#embed("bill-reports.typ")

Hope that's the right place for this comment

@NiklasEi

Copy link
Copy Markdown
Contributor Author

#embed is causing pagebreaks for me when using before #set page("a4") or after #pagebreak(weak: true).

I can reproduce this.

The current implementation implements Show for the embedding to get it into a frame item and have access to the file embedding during export. This follows the image implementation, but it seems strange because the embedding has no visual component. What would be a better alternative here to get relevant data to the export?

I looked at ' Tag ' while looking for other elements that might be closer to embedded files. Would adding a new variant to the Child enum in collect.rs make sense?

@NiklasEi

NiklasEi commented Nov 3, 2024 •

Copy link
Copy Markdown
Contributor Author

@sirchnik the implementation was changed with some pointers from Laurenz on Discord. The layouting issues should be fixed now.

One thing I haven't managed so far, but I still plan to do, is to move the embed methods into a PDF scope (so it's #pdf.embed(test.txt) and #pdf.embed.decode("some file content")).

@laurmaedje

Copy link
Copy Markdown
Member

I would prefer to drop the decode variant. We plan to deprecate the decode functions and instead allow passing bytes directly to the individual functions instead.

@laurmaedje

Copy link
Copy Markdown
Member

@NiklasEi I refactored this quite a bit together with @reknih, in part to make things more idiomatic, but also fixing some issues. Since dealing with the PDF specs is always quite involved, directly making the changes was a bit less time-consuming for me than leaving comments and going back and forth over it. I tried to make all commits small and self-contained, so feel free to take a look at the changes and let me know if anything seems odd. I want to make one more pass over the code to check things, but other than that this should be pretty much ready to merge.

@NiklasEi NiklasEi left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@laurmaedje Thank you for taking the time!
I like the changes but have a small question.

Comment thread crates/typst-pdf/src/lib.rs Resolved
@laurmaedje
laurmaedje enabled auto-merge January 8, 2025 09:35
@laurmaedje

Copy link
Copy Markdown
Member

Thanks for your work on this!

@laurmaedje
laurmaedje added this pull request to the merge queue Jan 8, 2025
Merged via the queue into typst:main with commit 0a374d2 Jan 8, 2025
@laurmaedje laurmaedje mentioned this pull request Jan 8, 2025
@ghost

ghost commented Jan 26, 2025 •

Copy link
Copy Markdown

Is it possible to use a different filename without renaming the file?

From the source code I found out that removing the file date works by #set document(date: none)

E: I see, it is possible by using read(...) as second argument. But this probably reads the whole file into memory. But nonetheless it works.

@laurmaedje

Copy link
Copy Markdown
Member

The whole file will be read into memory either way.

@ghost

ghost commented Jan 26, 2025

Copy link
Copy Markdown

In typst it seems reading contents fully into RAM is common. But we can think of a solution where it gets chunked in fixed size blocks if I call something like embed("input_filename.ext", "internal_filename.ext").

@laurmaedje

Copy link
Copy Markdown
Member

Yes, it's common because Typst abstracts over the file system in a relatively high-level way. Chunking it all the way from reading to embedding (and compressing!) in the PDF would hugely increase the implementation complexity and there are no plans to do that.

@ghost

ghost commented Jan 26, 2025

Copy link
Copy Markdown

I understand the design decision. Btw: is compression optional? Many file formats are already compressed, additional compression can result in higher filesize and longer compilation time.

@laurmaedje

Copy link
Copy Markdown
Member

Currently, it will always compress.

@mrfragger

Copy link
Copy Markdown

Ultimately did this version for 0.13 of PDF embed enable using the same name as the pdf for the attachment.typ?

Important Document.pdf
embeds
Important Document.typ

@laurmaedje

Copy link
Copy Markdown
Member

@mrfragger There is currently no way to get the input file name so you need to either spell it out in your source file or provide it via sys.inputs.

@mrfragger

Copy link
Copy Markdown

quite impressed
one.typ 5.1MB
one.pdf 8.6MB

one_qpdf_embed_typ.pdf 10MB
one_pdf.embed_typst.pdf 10.1MB

Seems like compression efficiency improved a tad for final typst 0.13 compared to pre-release.

so attachment size is 1.4MB for qpdf and 1.5MB for typst native pdf.embed. No longer need qpdf as a dependency for embedded source typst files in pdf. Thanks for the hard work put into this.

Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF A-3 - embeding xml into the exported document

9 participants