Open joetinger opened 9 years ago
There is some info here on registering fonts but I have not tried this myself:
http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-9
There is also an example here but it would have to be built-in/passed in to the system:
That's the issue I'm having, it doesn't seem like it can be done unless I edit MvcRazorToPdf.cs
in the nuget package.
You're in luck. I happen to need that functionality. I'll expose it this week and it'll end up in the package. On Oct 20, 2014 12:38 PM, "joetinger" notifications@github.com wrote:
That's the issue I'm having, it doesn't seem like it can be done unless I edit MvcRazorToPdf.cs in the nuget package.
— Reply to this email directly or view it on GitHub https://github.com/andyhutch77/MvcRazorToPdf/issues/21#issuecomment-59794747 .
Ah cool thank you!
@ElanHasson Nice one, let me know how you get on.
I faced with problem that your solution doesn't support unicode symbols. That's why russian letters don't appear in pdf. But I've solved it with custom FontFactory and sypplied it to the ParseXHtml method. I hope it will appear in the package soon.
@SergeyVitkevich Hi, is there any chance you could submit your changes for this? I will include it in the package if you think it is worth it.
It doesn't seem like this ended up in the package after all. Is there any news or someone who could point me in the right direction.
For anyone coming across this. Below is an edited version of the GeneratePdfOutput method in the MvcRazorToPdf class which will allow you register fonts using the FontFactory, E.G. FontFactory.Register("<path to font>.ttf");
public byte[] GeneratePdfOutput(ControllerContext context, object model = null, string viewName = null, Action<PdfWriter, Document> configureSettings = null)
{
if (viewName == null)
{
viewName = context.RouteData.GetRequiredString("action");
}
context.Controller.ViewData.Model = model;
byte[] output;
using (var document = new Document())
{
using (var workStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, workStream);
writer.CloseStream = false;
if (configureSettings != null)
{
configureSettings(writer, document);
}
document.Open();
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();
}
}
}
return output;
}
I've modify the source code follow to @nickyvadera. But the font is still remained Helvetica default font. What exactly should i do to change the font? I also did run FontFactory.Register("MYFONTPATH.ttf"); but the result is the same. Please advise.
After I register a font, what do I need to do to make it available as a style in the View?
public ActionResult DisplayPDF(int id)
{
var model = _getModel(id);
// not sure what the next steps are to make it available to the view
FontFactory.Register(HostingEnvironment.MapPath("~/...path-to-EBGaramond08-Regular.ttf"), "Garamond");
var pdf = new PdfActionResult("DisplayPDF", model, (writer, document) =>
{
// how do you make the new font "Garamond" available as a style in the view?
});
return pdf;
}
Is there anyway to get a custom font to work? I have one in my project that works in the HTML but I can't get it to work in the PDF
Thank you!