3

I am trying to add a custom font to my pdf output using the nuget package MVCRazorToPdf but I am having trouble with how to do this as the documentation for iTextSharp isn't great and all seems to be outdated.

The current code I have for creating the pdf is:

return new PdfActionResult(
    "test.cshtml", 
    new TestModel(),
    (writer, document) =>
    {
        FontFactory.Register(HostingEnvironment.MapPath("~/content/fonts/vegur-regular-webfont.ttf"), "VegurRegular");
    });

Where writer is a PdfWriter and document is a Document

All the examples of using the FontFactory show that you need to use the XmlWorker but I don't have access to that, so I was wondering if there was any way to change the documents font using the writer or document?

I've seen that there is the document.HtmlStyleClass property but can't find anything about how to use this anywhere.

Any help with this would be greatly appreciated

Pete
  • 57,112
  • 28
  • 117
  • 166
  • Most of the itextpdf examples are for the Java version, but a developer shouldn't have any problems porting Java to C#. The itext specific things are identical between Java and C#. – Amedee Van Gasse Dec 10 '15 at 11:33
  • Xmlworker is also available as a NuGet package https://www.nuget.org/packages/itextsharp.xmlworker/ – Amedee Van Gasse Dec 10 '15 at 11:34
  • I have that installed - it gets downloaded as part of the MvcRazorToPdf package – Pete Dec 10 '15 at 11:57
  • I am confused, in your question you wrote that you do not have access to xmlworker? – Amedee Van Gasse Dec 10 '15 at 12:10
  • The mvctrazortopdf package converts the razor view to a pdf using the above function, it only exposes the writer and the document and does all the worker stuff behind the scenes. – Pete Dec 10 '15 at 12:20

1 Answers1

4

MVCRazorToPdf is a very, very simple wrapper around iTextSharp's XMLWorker and uses the even simpler XMLWorkerHelper with all defaults to do its work. If you look at the source you'll see this:

                document.Open();


                using (var reader = new StringReader(RenderRazorView(context, viewName)))
                {
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);

                    document.Close();
                    output = workStream.ToArray();
                }

If you're dead-set on using the NuGet version then you're stuck with this implementation and you're not going to be able to register a custom font.

However, there's an open issue regarding this that includes a fix so if you're willing to compile from source you can apply that change and you should be all set.

If you want to go one step further I'd recommend reading this great post that shows how simple parsing HTML with iTextSharp is as well Bruno's post here that shows how to register fonts.

EDIT

As per the post in the includes a fix link (just in case the link breaks in future), change the above using statement to:

        using (var reader = new MemoryStream(Encoding.UTF8.GetBytes(RenderRazorView(context, viewName))))
        {
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader, null, FontFactory.FontImp as IFontProvider);

            document.Close();
            output = workStream.ToArray();
        }

And then the font factory as registered in the question above will work when using style="font-family:VegurRegular;"

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Brilliant, that's exactly what I needed - I had to edit the source anyway as I needed to merge it with an existing pdf, I just couldn't figure out how to change that xmlworkerhelper to use the new fontfactory – Pete Dec 10 '15 at 14:36