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

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()
		strWriterHTML.Close()
		strWriterHTML.Dispose()
		Server.Execute(cssFileLocation, strWriterCSS)
		strCSS = strWriterCSS.ToString()
		strWriterCSS.Close()
		strWriterCSS.Dispose()

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

		Try
			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))
			docWorkingDocument.Open()

			Dim fontProvider As New XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS)
			XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, strmHTML, strmCSS)
		Catch ex As Exception
			Response.Write(ex.Message)
		Finally
			If Not docWorkingDocument Is Nothing Then
				docWorkingDocument.Close()
				docWorkingDocument.Dispose()
			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!  

Thanks,
Jamie
LVL 1
Jamie FellrathApplication Developer/Data AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robberbaron (robr)Commented:
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  https://wkhtmltopdf.org/; 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 AnalystAuthor Commented:
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.
Robberbaron (robr)Commented:
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,
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

Jamie FellrathApplication Developer/Data AnalystAuthor Commented:
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.
Robberbaron (robr)Commented:
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 AnalystAuthor Commented:
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.  

https://code.msdn.microsoft.com/Convert-from-HTML-to-PDF-09ce2a1d
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.