iTextSharp-based HTML to PDF process doesn't display added fonts correctly on web page.

Jamie Fellrath
Jamie Fellrath used Ask the Experts™
I created a VB.NET web page that uses the iTextSharp functionality to transform an HTML page to a PDF dynamically.  

The name of the page is feespdf.aspx and it transforms the page FeesForPDF.aspx to a PDF when requested by the user on the page via a clickable link. No parameters are passed.

The problem I'm having is that our marketing department has given me some fonts that they want to use on the PDF - not a problem for simply displaying the ASPX page by using @font-face tags in the CSS file.  But the OTF-formatted font they gave me is not displaying on the PDF. The text that the OTF font is used on is defaulting to Arial.   The other font is an .SVG file and it came with two CSS files that define it (and are referenced on FeesForPDF.aspx). The SVG font works fine, the OTF font is ignored.  

Here's the code of my sub that does the PDF generation.  
	Protected Sub ProcessPDF()

		Dim docWorkingDocument As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 25, 25, 25, 25)

		Dim cssFileLocation As String = ConfigurationManager.AppSettings("PDFPrintCSSlocation").ToString()
		Dim strHtml As String
		Dim strCSS As String
		'Dim memStream As New MemoryStream()
		Dim strWriterHTML As New StringWriter()
		Dim strWriterCSS As New StringWriter()

		Server.Execute("FeesForPDF.aspx", strWriterHTML)
		strHtml = strWriterHTML.ToString()
		Server.Execute(cssFileLocation, strWriterCSS)
		strCSS = strWriterCSS.ToString()

		Dim strFileShortName As String = "CollegeAdvantage_Fees.pdf"
		Dim strFileName As String = HttpContext.Current.Server.MapPath("reports\" & strFileShortName)
		'Dim srdDocToString As StringReader = Nothing

			Dim pdfWrite As PdfWriter
			pdfWrite = PdfWriter.GetInstance(docWorkingDocument, New FileStream(strFileName, FileMode.Create))
			'srdDocToString = New StringReader(strHtml)
			Dim strmHTML As New MemoryStream(Encoding.UTF8.GetBytes(strHtml))
			Dim strmCSS As New MemoryStream(Encoding.UTF8.GetBytes(strCSS))

			Dim fontProvider As New XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS)
			XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, strmHTML, strmCSS)
		Catch ex As Exception
			If Not docWorkingDocument Is Nothing Then
			End If
		End Try

		Dim strPDFURL As String = ConfigurationManager.AppSettings("ContentPath") & "/reports/" & strFileShortName
		Dim strJavascript As String = "location.assign(""" & strPDFURL & """);"

		Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "myscript", strJavascript, True)

	End Sub

Open in new window

And here's the code I'm using in my CSS file to define the OTF font.  I've also tried it with a .WOFF version and neither works.  

 @font-face {
     font-family: 'Klinic_Slab';
     /*src: url('/content/fonts/klinicslabbook-webfont.woff') format('woff');*/
     src: url('/content/fonts/KlinicSlabBook.otf') format('opentype');
	 font-weight: normal;
	 font-style: normal;
 @font-face {
     font-family: 'Klinic_Slab';
     /*src: url('/content/fonts/klinicslabbold-webfont.woff') format('woff');*/
     src: url('/content/fonts/KlinicSlabBold.otf') format('opentype');
	 font-weight: bold;
	 font-style: normal;

Open in new window

If anyone has experience with iTextSharp and this sort of thing, please help me out here!  

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
it will be a font problem as likely the otf cant be translated into a postscript version.
you can try setting an alternate font that is very similar to your special, and that should be use in the pdf.

but also try using wkhtml2pdf; i have much better success using that to save my html formatted pages to pdf, with colors, rotated text etc.  At least that may help narrow the issue.
Jamie FellrathApplication Developer/Data Analyst


Thanks Robberbaron,

I should have been more clear - I'm not looking for a command line tool, I'm looking for functionality for our organization's public website to be able to generate PDFs from a web page on the fly - the source web page is dynamic.
i understand , but i use html2pdf from an intranet website.

try the commandline option to see if does a better job of converting your html to pdf that itextsharp before investigating further,
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jamie FellrathApplication Developer/Data Analyst


I was able to get it to work (and with less overhead and hassle) using SelectPDF functionality from an MSDN page. Thanks for everyone's help.
will have to look into that library also if it works for you.  I background load the html2pdf but it is still a blocking action sometimes.
Jamie FellrathApplication Developer/Data Analyst


It works surprisingly well - lots less overhead than the itextsharp stuff (which does a lot more than just HTML to PDF work, in its defense), fewer commands, etc.  I'm pleased.  

This is the "community" version I'm using - PDFs of no more than five pages allowed.  There's also a paid version that removes that limitation but that's fortunately more than we need.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial