6

NOTES: My request covers software or libraries, hence why I'm posting here. I also checked the similar threads here, but they asked for something subtly different.

I have the following HTML page:

<html>
<head>
   <link rel="stylesheet" type="text/css" href="font.css">
   <style>
      body {
        font-family: "Gotham SSm A";
        font-size: 22px;
      }
   </style>
</head>
<body>
   SUMMARY
</body>
</html>

And the font definition in font.css (truncated for brevity):

@font-face { 
   font-family: "Gotham SSm A"; 
   src: url(data:font/truetype;base64,...) format('truetype'); 
   font-weight:700; 
   font-style:italic; 
}

The page shows up fine in the browser, and when printed to PDF from the browser, is rendered fine as well. However, every utility I used to generate a PDF from server-side software (PHP) failed:

  1. Wkhtmltopdf messed up the fonts.
  2. PhantomJS messed up the fonts.
  3. SlimerJS failed to render, opened windows, and had unacceptable dependencies
  4. PrinceXML messed up the fonts and failed to parse all the CSS rules
  5. pandoc only converts to LaTeX and requires different utilities (on Windows/Linux) to go to PDF. What's more, it's LaTeX conversion (according to the online version I trie) messed up the fonts as well.

What are my alternatives? I need this to...

1. Respect modern CSS (including @font-face).
2. Be available on Windows & Linux with similar output on both
3. Be offline (utility or library is fine)
4. Allow commercial use
5. Be cost effective (preferably free)
2

I couldn't get the CSS sample you gave to do anything in my browser so was unable to test it but I suggest that you try pandoc.

  1. Respects CSS including @font-face - it actually uses this itself
  2. Available on Windows, Linux & Mac output will be similar providing the fonts are all available but you can use css to override the selection of fonts if necessary.
  3. Offline - definately
  4. Allow Commercial Use - Pandoc is free software, released under the GPL. © 2006-2014 John MacFarlane. AFAIK This means that while you cannot modify pandoc for commercial processes there is no reason not to use it for commercial purposes.
  5. It is free.
  6. It works in batch mode from the command line and doesn't open any windows.
  7. It can parse a large number of input formats and output to a much larger number of formats.

Update

Note that without the actual URL of the font in the CSS I am unable to test your sample even in a browser. I would recommend having a fallback using font stacks in your CSS. I have managed to find one online source for the font you are specifying but it is a pay to use option so most here will not be able to help I suspect.

It is strongly suggested that you use font stacks to provide fallback options. Recommended fallbacks are “Helvetica Neue”, Helvetica, Arial, sans-serif.

Changing your example to:

<html>
<head>
   <link rel="stylesheet" type="text/css" href="font.css">
   <style>
      body {
        font-family: "Gotham SSm A", “Helvetica Neue”, Helvetica, Arial, sans-serif;
        font-size: 22px;
      }
   </style>
</head>
<body>
   SUMMARY
</body>
</html>

Or installing the font locally may well help.

  • Thanks, but it doesn't convert to PDF; it converts to LaTeX, then requires a LaTeX->PDF converter. So even if that conversion is platform-uniform, there's still the platform-uniformity of the LaTeX->PDF converter which is highly unlikely since different packages are recommended for this, and it's already unlikely that the same conversion package will behave uniformly. I'll keep pandoc in mind as a last resort, as time permits. – R. Barzell Dec 8 '14 at 14:38
  • Just tried its online conversion to LaTex (the step towards PDF), and it didn't recognize the font. The offline version may be different, but this is so vanishingly unlikely that I'm dismissing pandoc as an option and modifying my question to add it to the exclusion list. – R. Barzell Dec 8 '14 at 14:45
  • @R.Barzell The reason that it doesn't give the "Correct" font on the online version is simple - the server doesn't have the font installed - it is not the most common font in the world. On "your" machine that font is obviously installed - Neither apaddedcell.com/web-fonts nor media.24ways.org/2007/17/fontmatrix.html list it so it is unlikely to be recognised on the web see smashingmagazine.com/2009/09/22/… and w3schools.com/cssref/css_websafe_fonts.asp for more. – Steve Barnes Dec 8 '14 at 18:42
  • Actually, I put the style sheet (which has the font definition inline) on a publicly available server and changed the link to a url, and ensured it was picked up in a non-pandoc baseline test -- so that's not the problem. I know about fallbacks, but that's not really relevant since the fonts are available. If fallbacks were good enough, I wouldn't be using custom fonts :P – R. Barzell Dec 8 '14 at 21:42
2

Pandoc has already been mentioned...

...but PrinceXML is also a strong contender.

While Pandoc is Free Software, PrinceXML is proprietary (though free to use for private purposes). PrinceXML is really strong when it comes to CSS support, and it does only HTML/XML/CSS conversion to PDF (without requiring any other external libraries).

Pandoc requires LaTeX to create PDFs, but Pandoc also can achieve a lot of other document format conversions (you could call it the "Swiss army knife" of document conversions).


Update:

Just noticed in the update of the OP, that he had already tested PrinceXML and was disappointed. This could probably be debugged if the input file(s) were available somehow in order to reproduce it.

Pandoc he has also tried... yes, on Windows it requires an additional MikTeX installation so it can convert HTML to PDF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.