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

Custom font with spaces in name causes invalid PDF #3000

Closed
Ceremony64 opened this issue Nov 12, 2020 · 5 comments · Fixed by #3018
Closed

Custom font with spaces in name causes invalid PDF #3000

Ceremony64 opened this issue Nov 12, 2020 · 5 comments · Fixed by #3018

Comments

@Ceremony64
Copy link

When generating a PDF and a custom font title with a space in its name is used, the resulting PDF is not PDF/A compliant and even fails to open in some PDF readers (such as awful Adobe Acrobat Reader).

Here is a example with jsPDF v2.11 and the default windows fonts Calibri and Comic Sans. From my testing, any ttf font will do:

var doc = new jsPDF(); // create document

doc.text("This is the default font", 100, 20, "center"); // write text
doc.save("Standard.pdf"); // save pdf

doc.addFont("https://server/calibri.ttf", "Calibri", "normal"); // embed font
doc.setFont("Calibri"); // set font
doc.text("This is Calibri", 100, 40, "center"); // write text
doc.save("Standard with Calibri.pdf"); // save with a custom font embedded

doc.addFont("https://server/comicsans.ttf", "Comic Sans", "normal"); // embed font
doc.setFont("Comic Sans"); // set font
doc.text("This is Comic Sans with a space in its name", 100, 60, "center"); // write text

doc.save("Standard with Calibri and Comic Sans.pdf"); // save with a custom font embedded

Using https://www.pdf-online.com/osa/validate.aspx to test the invalid PDF shows the following:

Validating file "Standard with Calibri and Comic Sans.pdf" for conformance level pdf1.3
	The "endobj" keyword is missing.
	The key Encoding is required but missing.
	The key DescendantFonts is required but missing.
	The document does not conform to the requested standard.
The file format (header, trailer, objects, xref, streams) is corrupted.
The document doesn't conform to the PDF reference (missing required entries, wrong value types, etc.).
The document does not conform to the PDF 1.3 standard.

Simply removing the space, making it "ComicSans" instead, fixes this issue and neither the validator or Acrobat Reader will complain anymore.

It is probably related to issue #2593

@JeBuSBrian
Copy link

I can confirm this scenario and that removing spaces fixes it. I came here looking for a solution and this provided it, relatively painlessly. Thanks @Ceremony64

@HackbrettXXX
Copy link
Collaborator

Here, the font.fontName also needs to be escaped like in the other occurrences. @Ceremony64 could you provide a pull request?

HackbrettXXX added a commit to HackbrettXXX/jsPDF that referenced this issue Dec 3, 2020
@HackbrettXXX
Copy link
Collaborator

I created a PR: #3018. @Ceremony64 could you verify that this fixes the issue? Note, that you need to run npm run build to update the files in the dist folder.

@cvharris
Copy link

cvharris commented Jun 1, 2021

This still isn't working and I'm using version 2.3.1. Is it expected we use the new fontFaces option for html()?

What's super weird is the PDF works fine if opened in Chromes' PDF viewer, but it does not work in any other browser nor on desktop like Mac Preview or Adobe. And when I punch in the file to the validator linked above by OP it also gives me the same error.

@HackbrettXXX
Copy link
Collaborator

@cvharris this was fixed (again) in #3149 but is not yet released.

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 a pull request may close this issue.

4 participants