MVCRazorToPdf (iTextSharp) 使用自定义字体
MVCRazorToPdf (iTextSharp) using custom font
我正在尝试使用 nuget 包 MVCRazorToPdf 将自定义字体添加到我的 pdf 输出中,但我在执行此操作时遇到了问题,因为 iTextSharp 的文档不是很好,而且似乎都已过时。
我当前用于创建 pdf 的代码是:
return new PdfActionResult(
"test.cshtml",
new TestModel(),
(writer, document) =>
{
FontFactory.Register(HostingEnvironment.MapPath("~/content/fonts/vegur-regular-webfont.ttf"), "VegurRegular");
});
其中 writer
是 PdfWriter
而 document
是 Document
所有使用 FontFactory
的示例都表明您需要使用 XmlWorker
但我无法访问它,所以我想知道是否有任何方法可以更改文档字体使用 writer
或 document
?
我看到有 document.HtmlStyleClass
属性 但找不到任何关于如何在任何地方使用它的信息。
如有任何帮助,我们将不胜感激
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 你会看到这个:
document.Open();
using (var reader = new StringReader(RenderRazorView(context, viewName)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
document.Close();
output = workStream.ToArray();
}
如果您执意要使用 NuGet 版本,那么您将无法使用此实现,并且无法注册自定义字体。
但是,有一个 open issue regarding this that includes a fix so if you're willing to compile from source 您可以应用该更改,您应该已经准备就绪。
如果您想更进一步,我建议您阅读显示如何注册字体的 this great post that shows how simple parsing HTML with iTextSharp is as well Bruno's post here。
编辑
根据包含修复程序 link 中的 post(以防将来 link 中断),将上述 using
语句更改为:
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();
}
然后上面问题中注册的字体工厂在使用style="font-family:VegurRegular;"
时就可以工作了
我正在尝试使用 nuget 包 MVCRazorToPdf 将自定义字体添加到我的 pdf 输出中,但我在执行此操作时遇到了问题,因为 iTextSharp 的文档不是很好,而且似乎都已过时。
我当前用于创建 pdf 的代码是:
return new PdfActionResult(
"test.cshtml",
new TestModel(),
(writer, document) =>
{
FontFactory.Register(HostingEnvironment.MapPath("~/content/fonts/vegur-regular-webfont.ttf"), "VegurRegular");
});
其中 writer
是 PdfWriter
而 document
是 Document
所有使用 FontFactory
的示例都表明您需要使用 XmlWorker
但我无法访问它,所以我想知道是否有任何方法可以更改文档字体使用 writer
或 document
?
我看到有 document.HtmlStyleClass
属性 但找不到任何关于如何在任何地方使用它的信息。
如有任何帮助,我们将不胜感激
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 你会看到这个:
document.Open();
using (var reader = new StringReader(RenderRazorView(context, viewName)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
document.Close();
output = workStream.ToArray();
}
如果您执意要使用 NuGet 版本,那么您将无法使用此实现,并且无法注册自定义字体。
但是,有一个 open issue regarding this that includes a fix so if you're willing to compile from source 您可以应用该更改,您应该已经准备就绪。
如果您想更进一步,我建议您阅读显示如何注册字体的 this great post that shows how simple parsing HTML with iTextSharp is as well Bruno's post here。
编辑
根据包含修复程序 link 中的 post(以防将来 link 中断),将上述 using
语句更改为:
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();
}
然后上面问题中注册的字体工厂在使用style="font-family:VegurRegular;"