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

Transparent Gradient does not export to PDF correctly #2546

Open
1 task done
schang412 opened this issue Nov 1, 2023 · 9 comments · May be fixed by #5420
Open
1 task done

Transparent Gradient does not export to PDF correctly #2546

schang412 opened this issue Nov 1, 2023 · 9 comments · May be fixed by #5420
Assignees
Labels
bug Something isn't working good contribution Good, isolated issue for someone that wants to contribute help wanted Extra attention is needed pdf Related to PDF export

Comments

@schang412
Copy link

Description

When applying a gradient with alpha, the PDF export puts a white background behind the gradient.

Preview on webapp behaves as expected
PNG export behaves as expected.
PDF export does not match preview nor PNG, it places a white background behind the gradient.

// nested rectangles
#rect(fill: rgb(50%, 50%, 0%))[
  #rect(fill: rgb(50%, 0%, 0%))[#h(100%)]
]

// transparent rectangles
#rect(fill: rgb(50%, 50%, 0%, 120))[
  #rect(fill: rgb(50%, 0%, 0%, 120))[#h(100%)]
]

// gradient has white background in PDF export
#let transparent = rgb(255, 255, 255, 0)
#rect(fill: rgb(50%, 50%, 0%, 100))[
  #rect(fill: gradient.linear(transparent, transparent, rgb(50%, 0%, 0%), transparent, transparent))[#h(100%)]
]

// for completeness, a color with a specified alpha value also fails
#rect(fill: rgb(50%, 50%, 0%, 100))[
  #rect(fill: gradient.linear(transparent, transparent, rgb(50%, 0%, 0%, 120), transparent, transparent))[#h(100%)]
]

I am not 100% familiar with how a PDF document works, but somewhere in the chain, I suspect that the gradient's transparency information is lost.

Reproduction URL

No response

Operating system

Web app

Typst version

  • I am using the latest version of Typst
@schang412 schang412 added the bug Something isn't working label Nov 1, 2023
@LaurenzV
Copy link
Collaborator

LaurenzV commented Nov 1, 2023

Unfortunately, adding opacities to gradients in PDF is much more complex than just adding an "opacity" value to it, so it's probably just not been implemented yet? @Dherse

@Dherse
Copy link
Collaborator

Dherse commented Nov 1, 2023

It should be doable to implement it, it just isn't because it's a whole rabbit hole of its own

@Enivex Enivex added the pdf Related to PDF export label Nov 2, 2023
@laurmaedje laurmaedje added help wanted Extra attention is needed good contribution Good, isolated issue for someone that wants to contribute labels Nov 14, 2023
@Dherse Dherse self-assigned this Nov 23, 2023
@Quantumplation
Copy link

Supposedly this was implemented in 0.7, as per #79, but I'm also having the same issue

@laurmaedje
Copy link
Member

Supposedly this was implemented in 0.7, as per #79, but I'm also having the same issue

The 0.7 change brought alpha support in general, but not for gradients (which only came with 0.9).

@Dherse
Copy link
Collaborator

Dherse commented Jan 25, 2024

I have a branch that sorts of work but it's far from merge ready.

@Losses
Copy link

Losses commented Aug 3, 2024

@LaurenzV @Dherse

Thank you for your efforts regarding the complexities involved in adding opacities to gradients in PDFs. I understand that it's a challenging task, potentially involving more than just adding an "opacity" value, and it might not have been implemented yet due to the intricate nature of the issue.

The issue resonates deeply with me. The inconsistencies between PDF output and preview results, particularly concerning transparency and SVG image rendering, have caused me significant trouble. Just yesterday, after completing an extremely complex template for our company, I faced a heartbreaking moment. Despite everything looking perfect in the preview, the output PDF was a chaotic mess. It was already past midnight, and I had been working overtime for over seven hours. Now I have to tackle with more issue on weekends.

I fully appreciate the technical difficulties involved in implementing features like SVG and transparency in PDFs. However, I wonder if we might consider a compromise to ensure visual consistency. For instance, could we output high-resolution bitmap PDFs (suitable for printing, as the current default PNG output resolution is quite low) and attach the text information in a hidden layer within the PDF, similar to what OCR documents do, which can enabling text selection?

While I'm not deeply familiar with the technical intricacies of PDF implementation, my intuition suggests that this approach might provide a more unified solution to the discrepancies between preview and actual PDF output.

And for now, if you are facing the same trouble, consider output all files to the SVG format and convert these files to PDF with a headless Chromium, here is an example: https://github.com/Losses/pdf-postprocess

In theory, rsvg can also achieve this goal, but I haven't yet found a method for Rust binding to output PDF.

Thank you for the amazing effort on typst, I really appreciate it.

@LaurenzV
Copy link
Collaborator

LaurenzV commented Aug 3, 2024

Despite everything looking perfect in the preview, the output PDF was a chaotic mess.

I mean, could you be a bit more specific? What exactly was wrong, apart from gradients with transparency not showing up correctly, which doesn't sound like something that should be that noticeable?

@Losses
Copy link

Losses commented Aug 3, 2024

more specific

Thank you for your prompt response.

Recently, most of the issues I have encountered are related to gradient rendering problems. Regarding SVG, I have faced two issues. One was with the rendering of SVG patterns, which seems to have been fixed as I can no longer reproduce it. The other issue is that some images cannot be rendered correctly as vector graphics. Here is an example: https://typst.app/project/r8-jUq5xEOsa1q21pqBHEn

@LaurenzV
Copy link
Collaborator

LaurenzV commented Aug 3, 2024

The reason this is happening is because your SVG contains filters. It's not possible to translate those to PDF, as PDF doesn't support them directly. So the only option is rasterizing the parts affected by the filter, which is what is happening here. You can try any other SVG to PDF converter, and you'll most likely see the same result, so there isn't much we can do here. It does look pixelated, so I guess we could provide an option to specify the resolution at which the parts should be rasterized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good contribution Good, isolated issue for someone that wants to contribute help wanted Extra attention is needed pdf Related to PDF export
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants