Skip to content

prevent font name collision corrupting _fontFamilies and PDF output#1706

Open
andreiaugustin wants to merge 3 commits intofoliojs:masterfrom
andreiaugustin:1651
Open

prevent font name collision corrupting _fontFamilies and PDF output#1706
andreiaugustin wants to merge 3 commits intofoliojs:masterfrom
andreiaugustin:1651

Conversation

@andreiaugustin
Copy link
Contributor

@andreiaugustin andreiaugustin commented Mar 20, 2026

Bug fix

What kind of change does this PR introduce?

This should fix #1651 - basically when a doc mixes standard and embedded fonts that share the same postscript name, as it is the case in the built-in Helvetica and Helvetica.ttf, the name-based alias in _fontFamilies was getting overwritten, causing one font to never be finalised and resulted in a corrupt PDF.

With this change, the name-based slot in _fontFamilies is now only written if it is not already occupied, preserving whichever font was registered first.

Furthermore, an extra safety net is added for fonts loaded via raw buffer with no cacheKey, whose ps name collides with already registered fonts, which would otherwise slip through both registration paths - it now gets registered under its unique id so it is always reachable during finalisation.

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

@andreiaugustin
Copy link
Contributor Author

Here's the code I've used to test for the extra safety net:

var fs = require('fs');
const PDFDocument = require('../');

var doc = new PDFDocument();
doc.pipe(fs.createWriteStream('buffer-font-name-collision.pdf'));

const buf = fs.readFileSync('fonts/Helvetica.ttf');
doc.font(buf);

doc.text('standard helvetica (before font switch)');
doc.font(buf);
doc.text('should be embedded helvetica (after font switch)');

doc.end();

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.

Mixing standard and embedded Helvetica font does not work

1 participant