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

File size increased hugely after embeded font through fontkit #207

Closed
Pomelool opened this issue Sep 29, 2019 · 5 comments
Closed

File size increased hugely after embeded font through fontkit #207

Pomelool opened this issue Sep 29, 2019 · 5 comments

Comments

@Pomelool
Copy link

Pomelool commented Sep 29, 2019

I have a pdf that is 600KB, after embeded a font (ttf file of 20MB), and drawed one line with this font, my generated pdf becomes 9mb. Is this a normal behavior? Or am I doing something wrong?

Is there a way to subsetting a font instead of embedding the whole font?

@Hopding
Copy link
Owner

Hopding commented Oct 1, 2019

Hello @Pomelool! This is normal. pdf-lib does not subset embedded fonts by default. Instead, it embeds the entire font into the document. However, there is an option to use subsetting when you embed the font. It is an option you must pass to PDFDocument.embedFont. For example:

const ubuntuBytes = ...
const ubuntuFont = await pdfDoc.embedFont(ubuntuBytes, { subset: true })

And here are some other examples:

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r_base64, {
subset: true,
});
page2.drawText(ipsumLines.join('\n'), {
y: size - 20,
size: 20,
font: ubuntuFont,
lineHeight: 20,
});
const fantasqueFont = await pdfDoc.embedFont(
fonts.otf.fantasque_sans_mono_bi,
);
page2.drawText(ipsumLines.join('\n'), {
y: size - 105,
size: 25,
font: fantasqueFont,
lineHeight: 25,
});
const indieFlowerFont = await pdfDoc.embedFont(fonts.ttf.indie_flower_r, {
subset: true,
});
page2.drawText(ipsumLines.join('\n'), {
y: size - 200,
size: 25,
font: indieFlowerFont,
lineHeight: 25,
});
const greatVibesFont = await pdfDoc.embedFont(fonts.ttf.great_vibes_r, {
subset: true,
});
page2.drawText(ipsumLines.join('\n'), {
y: size - 300,
size: 30,
font: greatVibesFont,
lineHeight: 30,
});
const appleStormFont = await pdfDoc.embedFont(fonts.otf.apple_storm_r);
page2.drawText(ipsumLines.join('\n'), {
y: size - 425,
size: 25,
font: appleStormFont,
lineHeight: 25,
});
const bioRhymeFont = await pdfDoc.embedFont(fonts.ttf.bio_rhyme_r, {
subset: true,
});
page2.drawText(ipsumLines.join('\n'), {
y: size - 500,
size: 15,
font: bioRhymeFont,
lineHeight: 15,
});
const pressStart2PFont = await pdfDoc.embedFont(fonts.ttf.press_start_2p_r, {
subset: true,
});

It's important to note that subsetting does not work for all fonts. This is why the feature is not enabled by default. The subsetting logic resides in the @pdf-lib/fontkit module, which is a fork of https://github.com/foliojs/fontkit. If the subsetting bug in fontkit is ever fixed, then I can pull it into pdf-lib and perhaps enable subsetting by default.

I hope this helps. Please let me know if you have any additional questions!

@vlio20
Copy link

vlio20 commented Jan 2, 2021

Thanks @Hopding . I suggest that you will add this to your documentation in the README.md file

@Hopding
Copy link
Owner

Hopding commented Jan 2, 2021

That's a good idea. It would make a nice addition to the https://github.com/Hopding/pdf-lib#fonts-and-unicode section. Happy to accept PRs for this!

@vlio20
Copy link

vlio20 commented Jan 2, 2021

Sure I will happy to that!

@vlio20
Copy link

vlio20 commented Jan 2, 2021

@Hopding , another question if I may. Is it possible to embed multiple fonts (to increase the chance of successful conversion)?

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

No branches or pull requests

3 participants