Avoid invalid PDF operators when drawing SVG text #1554
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was causing display problems and warning messages in some PDF software.
Example HTML
Before
PDF X-Change Viewer[1] doesn't render SVG text:
Adobe Acrobat Reader DC [2] renders SVG text:
but displays this warning message after printing (printing to a virtual printer is sufficient):
After
PDF X-Change Viewer renders SVG text:
Adobe Acrobat Reader DC now prints with no warning messages.
Root cause
I managed to narrow this down to the PDF content stream that's being emitted for the SVG text. I used pikepdf to parse it.
SVG text PDF content stream before (invalid operators marked with
**
):According to the PDF specification, "special graphics state" operators (q, Q, cm) and "path construction" operators (m) aren't permitted while a text object is being drawn. See page 134-135, Table 4.1 Operator categories, and Figure 4.1 Graphics objects.
PDF readers seem to be quite tolerant of invalid content streams in general, but apparently this was too much!
Content stream after:
[1] PDF-XChange Viewer 2.5 (Build 322.10) - https://www.tracker-software.com/product/pdf-xchange-viewer
[2] Adobe Acrobat Reader DC 2021.011.20039