使用iTextSharp的html到pdf生成的html字体没有变化 [英] No change in font face in html to pdf generation using iTextSharp
问题描述
我正在使用HTML Rich-Text编辑器来帮助我为动态PDF报告创建模板,并且除不改变字体外观外,它都可以正常工作.
I'm using a HTML Rich-Text Editor to help me create a template for dynamic PDF reports, and it's working fine except that it doesn't change the font-face.
此编辑器使用的是font标签而不是CSS样式,因此我欢迎采用任何方式以编程方式使用样式将字体标签更改为等效标签.
This editor is using the font tag instead of CSS styles, and I would welcome any way to programmatically change the font tags to equivalent tags using styles instead.
HTML(是的,杂乱无章,它来自所见即所得"编辑器):
HTML (yes its messy, its from a WYSIWYG editor):
<div>
<br>
<div align="center">
<font size="5">
<b>
<br>
<div align="center">
<font font-face="Times New Roman" size="5">
<b>Example
<font size="6">Chamber
<font size="5">
<font size="4">Website</font>
</font></font>Quotes</b>
</font>
<font face="Times New Roman">
<br>
<font face="Times New Roman">
<br>~
<font face="Times New Roman" color="#0000FF">
<b>
<u> [!PlanName] </u></b>
</font>
<font face="Times New Roman">
<br>~
<font face="Times New Roman" color="#0000FF">
<b> </b>
</font>
<font face="Times New Roman" color="#0000FF">
<b>
<font color="#000000">Deductible
$[!PlanDeductible]: </font></b>
</font>
<font face="Times New Roman" color="#B0B0FF">
[!PlanRate]
<br>
<font face="Times New Roman">/~/~</font>
<br>
<br>
<br>
<font face="Courier New" size="1">
Copyright Example.com</font>
<br>
<br>
<font face="Arial">test</font>
<br></br>
</br>
</br>
</br>
</br>
</br>
</br></font></br>
</font></br>
</font>
</br>
</font>
</div>
</br>
</b>
</font>
</div>
</br></div>
C#:
public static byte[] ConvertHtmlToPdf(string html)
{
html = HtmlPostProcessor.Process(html);
byte[] fileData = null;
string tempPath = ConfigurationManager.AppSettings["TempDirectory"];
string tempPDFFile = Path.Combine(tempPath, Guid.NewGuid() + ".pdf");
Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
{
PdfWriter.GetInstance(document, fs);
using (StringReader stringReader = new StringReader(html))
{
List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null );
document.Open();
foreach (IElement item in parsedList)
{
document.Add(item);
}
document.Close();
}
}
FileStream generatedPDF = File.Open(tempPDFFile, FileMode.Open);
fileData = new byte[(int)generatedPDF.Length];
int result = generatedPDF.Read(fileData, 0, (int)generatedPDF.Length);
generatedPDF.Close();
File.Delete(tempPDFFile);
return fileData;
}
编辑
我一直在使用iTextSharp版本5.1.1.0.
I've been using iTextSharp version 5.1.1.0.
推荐答案
The simplest, brute-force way is to call FontFactory.RegisterDirectories() before calling HTMLWorker.ParseToList()
. Be warned, however - the method attempts to register/map all fonts on the running system.
因此,例如,如果您在ASP.NET中运行此程序,则可能要将呼叫放在global.asax中.
So if you're running this in ASP.NET, for example, you probably want to put the call in global.asax.
编辑:使用FontFactory.RegisterDirectories()
和上面提供的HTML的工作示例:
EDIT: Working example using FontFactory.RegisterDirectories()
and the HTML you provided above:
FontFactory.RegisterDirectories();
using (Document document = new Document()) {
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
List<IElement> objects = HTMLWorker.ParseToList(
new StringReader(html), null);
foreach (IElement element in objects) {
document.Add(element);
}
}
只需将Response.OutputStream
替换为您选择的Stream
.从上面的文件
Just substitute Response.OutputStream
with the Stream
of your choice. Result PDF file from above.
这篇关于使用iTextSharp的html到pdf生成的html字体没有变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!