pdfjs icon indicating copy to clipboard operation
pdfjs copied to clipboard

Graphical state is not correctly reset when using templates

Open rkusa opened this issue 4 years ago • 3 comments

The test added here https://github.com/rkusa/pdfjs/commit/1c087a180f2133b49475feb75bcc42b76c6eff6c (#150) shows that the added template seem to effect the graphical state of the page content that comes on t of it:

Screenshot 2019-10-06 at 13 03 46

(the text should be bigger and not mirrored)

Reseting the state via Q or cm operators didn't seem to work, so another solutions has to be found.

rkusa avatar Oct 06 '19 11:10 rkusa

Is there already a solution in progress for this? Because I face the same problem in my project with this library.

MeisterBerg avatar Dec 06 '21 07:12 MeisterBerg

I also ran into the upside-down issue today, did someone figure out a workaround for this?

DASPRiD avatar Aug 10 '22 18:08 DASPRiD

Okay, so I played around a little with a minimal PDF created by Inkscape. It contains a single object on the page it exports, which decodes to this:

1 0 0 -1 0 841.889771 cm % Set matrix
q % Save graphics state
Q % Restore graphics state

So the issue here is that Inkscape (or rather, cairo) does not place the transformation matrix inside the graphics state, which leads to it bleeding into the objects created by pdfjs.

I'm not sure what the ideal solution to this issue might be. One option would be to explicitly set the transformation matrix in the root objects created by pdfjs. Another option would be to insert a q operation before the template, so that a Q operation would succeed after the template.

Just FYI: As a workaround you can use another tool to create a PDF which doesn't use a global transformation matrix.

DASPRiD avatar Aug 11 '22 15:08 DASPRiD

@DASPRiD thanks for digging into this! Should be fixed on main now. Solution was as suggested a q before and a Q after the template.

rkusa avatar Sep 05 '22 17:09 rkusa