pdffontsitexthtml-to-pdfxmlworker

iText XMLWorkerHelper font with CE characters


I have troubles with converting HTML to PDF with iText XMLWorkerHelper. Document is genereted fine but it end up without some central European characters (Croatian letters like č, ć, đ, ž).

When I write text without XMLWorkerHelper and define font same text from databese is written correctly:

Dim fntArial As BaseFont = BaseFont.CreateFont("Helvetica", BaseFont.CP1250, BaseFont.NOT_EMBEDDED)
Dim fontText10 As Font = New Font(fntArial, 10)

But when I use XMLWorkerHelper CE characters are lost.

Dim pdfDokument As New Document()
Dim docContent As String = "<style>" & _
                           "@font-face {font-family: 'Arial'; src: url('fonts/arial.ttf') format('truetype');}" & _
                           "body, td, div, span{font-family: 'Arial'; color: #253f60; font-size: 12px;}" & _
                           ...
                          "</style>" & _
                          "HTML content from database"
Dim wrtPDF As PdfWriter = PdfWriter.GetInstance(pdfDokument, New FileStream(Request.PhysicalApplicationPath & docName, FileMode.Create))
pdfDokument.Open()
Dim worker As XMLWorkerHelper = XMLWorkerHelper.GetInstance
worker.ParseXHtml(wrtPDF, pdfDokument, New StringReader(docContent))

How can I embed CE font to PDF document and set is as default font for XMLWorkerHelper?


Solution

  • I finally figured out how to provide font that I want to use. First I specified full font path including font itself (like c:/windows/fonts/arial.ttf) and nothing has changed, then I tried with different encoding and evenetually I passed just path to fonts folder which solved my problem (after I read XMLWorkerFontProvider class definition).

    Chris thx you pointed me to right path.

    Dim worker = XMLWorkerHelper.GetInstance()
    Dim htmlContent = new MemoryStream(Encoding.UTF8.GetBytes(docContent))
    Dim css As memoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(""))
    worker.ParseXHtml(wrtPDF, pdfDokument, htmlContent, css, System.Text.Encoding.UTF8, new XMLWorkerFontProvider("c:/windows/fonts/"))