31
votes

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 (http://blogs.msdn.com/b/donovans/archive/2007/07/20/reporting-services-pdf-renderer-faq.aspx 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).

4
Could you give the names of the fonts that you need to be embed? (Some fonts do have licenses which don't allow embedding, and most PDF processing or creating software does honor the respective flags in the font files and choose to not embed t hem...)Kurt Pfeifle
Sure! Georgia and Calibri - I've checked that we have TrueType versions and (well, according to Windows) they both have the 'Font embedability' tag set to 'Editable' - I gather this is the right setting? Thanks for any help!hanzworld
For anyone who reads this later on, both of these methods worked. SQL Reporting Services finally embedded fonts correctly after many updates / hotfixes and could be sent straight to the printer. Additionlly, called Ghostscript using ProcessInfo from .NET could post-process the PDF.hanzworld

4 Answers

36
votes

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.

15
votes

Besides Ghostscript, it is also possible to use Poppler and Cairo. There is a command pdftocairo from Poppler that converts PDF to PDF via pdftocairo -pdf input.pdf output.pdf. It also considers font substitutions set in a Fontconfig configuration file. This is very helpful if you do not have all fonts on your system that are referenced in a PDF file, but know which other font you have installed is a good-looking replacement. After processing, the substitution font is embedded.

1
votes

I had this problem today with an existing PDF I uploaded to lulu.com to make a printed copy. It was rejected for not having all fonts embedded.

I found that if I opened it in Acrobat X and Saved out as postscript .ps file, then when I double clicked this .ps file in File Explorer, it opened in Acrobat X Distiller, and this automatically created a new PDF file with all fonts embedded!

Naturally this would mean you must have all the fonts needed on your computer. Otherwise a program like InFix can make font substitutions.

0
votes

I had this problem on a Mac with a PDF I was submitting to IEEE. Using Adobe Reader and Preview, I was able to get around this. I think any pdf printer might work in place of Preview if you are on a PC.

Here are the steps I took. You can individually fix each figure, or fix the whole document.

  1. Open at pdf file using Adobe Reader.

  2. Right click on image, and click “Document Properties.”

  3. Click “Fonts.” Check to see if the font isn’t embedded. Should say “Courier” or other font name.

  4. If your pdf isn’t a standard page size, click on “Description” and look at the page size. Write this down. Ex. 19.4 x 5.22 in.

  5. Open the pdf up in Preview. Go to File->Print. If using a pdf that isn’t a standard page size, click on Paper Size and choose custom. You will need to create a custom page size that is equal to the one you wrote down in step 4. Don’t forget to zero the margins to 0 for all sides. After doing that, you’ll need to set the scale of the print in the print dialog to 100%.

  6. In the lower left of the print dialog (in Preview on a Mac), click “PDF” to print the PDF to a new PDF. Select the destination and print.

  7. Open the new pdf up in Adobe Reader and verify that the fonts are now embedded.

I hope this helps.