Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I embed fonts in an existing PDF?

Background:

I have PDF's I am programmatically generating. I need to be able to send the PDF directly to a printer from the server (not through an intermediate application). At the moment I can do all of the above (generate PDF, send to printer), but because the fonts aren't embedded in the PDF the printer is doing font substitution.

Why the fonts aren't embedded when generated:

I am creating PDF's using SQL Reporting Services 2008. There is a known issue with SQL Reporting Services in that it will not embed fonts (unless a series of requirements are met - http://technet.microsoft.com/en-us/library/ms159713%28SQL.100%29.aspx). Don't ask me why, the PDF meets all of MS's listed requirements and the fonts still show up as not embedded - there is no real control over whether the fonts are embedded, so I have accepted that this isn't working and moved on. The suggested workaround from Microsoft (Link under 'When will Reporting Services do font embedding') is to post process the PDF to manually embed the fonts.

Goal Take an already generated PDF document, programmatically 'open' it and embed the fonts, resave the PDF.

Approach I was pointed towards iTextSharp, but most of the examples are for the Java version and I'm having trouble translating to the iTextSharp version (I can't find any documentation for iTextSharp).

I am working on this post for what I need to do: Itext embed font in a PDF.

However for the life of me, I cannot seem to use the ByteArrayOutputStream object. It can't seem to find it. I've researched and researched but nobody seems to say what class it's in or where I find it so I can include it in the using statements. I've even cracked open Reflector and can't seem to find it anywhere.

This is what I have so far and it compiles etc. etc. (result is my byte[] of the generated PDF).

PdfReader pdf = new PdfReader(result);             BaseFont unicode = BaseFont.CreateFont("Georgia", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); // the next line doesn't work as I need a ByteArrayOutputStream variable to pass in PdfStamper stamper = new PdfStamper(pdf, MISSINGBYTEARRAYOUTPUTSTREAMVARIABLE); stamper.AcroFields.SetFieldProperty("test", "textfont", unicode, null);  stamper.Close(); pdf.Close(); 

So can anybody either help me with using iTextSharp to embed fonts into a PDF or point me in the right direction?

I'm more than happy to use any other solutions other than iTextSharp to complete this goal, but it needs to be free and able to be used by a business for an internal application (i.e. Affero GPL).

like image 895
hanzworld Avatar asked Nov 20 '10 07:11

hanzworld


People also ask

Why are fonts not embedded in PDF?

When a font cannot be embedded because of the font vendor's settings, and someone that opens or prints the PDF does not have access to the original font, a Multiple Master typeface is temporarily substituted–AdobeSerifMM for a missing serif font, and AdobeSansMM for a missing sans serif font.

How do I fix PDF fonts not embedded in a PDF?

In Acrobat Pro, Tools > Print Production > Preflight > expand “PDF Fixups” > select “Embed Fonts” > click “Analyze and fix”.

Are fonts automatically embedded in PDF?

If you are using Adobe Acrobat to create your PDF then the latest versions will automatically embed any suitable fonts. In earlier versions you can turn this on or off, so make sure you have this ticked (see the diagram below). Also check that the fonts you are using are listed in the Font Source.

Does Microsoft Word embed fonts in PDF?

If you're using only the File>Save As>PDF feature of Word, choose the Standard (publishing online and printing) option in the Save As dialog. This will embed a subset of the font in the PDF, meaning that only the font characters actually used in your document are embedded.


1 Answers

This may not be the answer you are looking for (since you want to get your problems solved programmatically, not by an external tool).

But you can use Ghostscript commandline to embed missing fonts in retrospect to PDFs which have not embedded them:

gs \   -sFONTPATH=/path/to/fonts:/another/dir/with/more/fonts \   -o output-pdf-with-embedded-fonts.pdf \   -sDEVICE=pdfwrite \   -dPDFSETTINGS=/prepress \    input-pdf-where-some-fonts-are-not-embedded.pdf 

One important thing is that the missing fonts are all available in one of the directories pointed to by the -sFontPath=... switch.

like image 155
Kurt Pfeifle Avatar answered Sep 23 '22 22:09

Kurt Pfeifle