为什么我的PDF生成为空白?
我正在使用ItextSharp和c#,asp.net MVC来生成PDF报告。但是,当我生成报告时,PDF返回为空白(除了正常工作的标题)。我很乐意为您提供意见。
I am using ItextSharp and c#, asp.net MVC to generate a PDF report. However, when I generate the report the PDF comes back as blank (apart from a header which is working fine). I would love your input.
生成报告的代码如下:
using (var writer = PdfWriter.GetInstance(doc, ms))
{
// This sorts out the Header and Footer parts.
var headerFooter = new ReportHeaderFooter(reportsAccessor);
writer.PageEvent = headerFooter;
var rootPath = ConfigurationManager.AppSettings["SaveFileRootPath"];
string html = File.ReadAllText(rootPath + "/reports/report.html");
// Perform the parsing to PDF
doc.Open();
// The html needs to be sorted before this call.
StringReader sr = new StringReader(html);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr);
doc.Close();
writer.Close();
res = ms.ToArray();
}
我知道有很多隐藏的,但为了论证,一个例子是这个生成并放入StringReader的HTML在这里:
I know there is a lot hidden, but for arguments sake an example of the HTML that this generates and puts in the StringReader is here:
<table style="font-size:10pt">
<tr>
<td rowspan="4"> Address<br/> </td>
<td>Phone: phone</td>
</tr>
<tr>
<td>Fax: fax</td>
</tr>
<tr>
<td>Email: email@example.com</td>
</tr>
<tr>
<td>Website: example.com</td>
</tr>
<table>
<table style="font-size:10pt; width:100%">
<tr style="width:50%">
<td>Settlement: 30 days from invoice</td>
</tr>
<tr>
<td>Delivery Charge Notes: N/A</td>
</tr>
</table>
<p style="width:100%; font-size:10pt">
I love notes</p>
<table>
<tr style="font-weight:bold">
<td>Item</td>
<td>Item Code</td>
<td>Description</td>
<td>Unit Size</td>
<td>Units Per Outer</td>
<td>Units Per Pallet</td>
<td>Invoice Price Volume Breaks</td>
<td>Branding</td>
<td>Notes</td>
</tr>
</table>
但是,这个html会生成一个漂亮的空白PDF文件(不是我想要的)。我看不出这可能有什么问题,并且会喜欢两件事:
However, this html generates a nice blank PDF file (not what I want). I can't see what might be wrong with this and would love some input in to two things:
[1]为什么报告是空白的
[2]如果这是解析中的一个错误,为什么它没有出现错误而是一个空白报告(我可以设置一个设置或参数,它会抛出比空白报告更有用的错误吗?)
[1] Why the report is blank [2] If this is an error in the parsing, why it doesn't come up with an error and instead a blank report (is there a setting or argument I can set that will throw errors which are much more useful than blank reports?)
更新:
我添加了页眉/页脚的代码
UPDATE: I have added the code for the header/footer
public string HeaderTitle {get ;组; }
public IReportsAccessor ReportsAccessor {get;组; }
public ReportHeaderFooter(IReportsAccessor reportsAccessor)
{
this.ReportsAccessor = reportsAccessor;
}
public string HeaderTitle { get; set; } public IReportsAccessor ReportsAccessor { get; set; } public ReportHeaderFooter(IReportsAccessor reportsAccessor) { this.ReportsAccessor = reportsAccessor; }
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
var rootPath = ConfigurationManager.AppSettings["SaveFileRootPath"];
GetReportImageResult imgInfo = ReportsAccessor.GetImage(4);
byte[] header_img = imgInfo.ReportImage;
string logoFn = rootPath + "/tmp/logo.png";
File.WriteAllBytes(logoFn, header_img);
string html = File.ReadAllText(rootPath + "/reports/report_header.html");
html = html.Replace("{{ title }}", HeaderTitle);
html = html.Replace("{{ logo_img }}", logoFn);
StringReader sr = new StringReader(html);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
}
当我运行你的HTML时我的代码,我确实得到了一个例外,因为你的HTML中有一个偷偷摸摸的错误。第一个表没有结束标记:它表示< table>
而不是< / table>
。这很容易被忽视。您可以在此处找到更正的HTML: table10.html
When I run your HTML through my code, I do get an exception, because there's one sneaky error in your HTML. The first table doesn't have a closing tag: it says <table>
instead of </table>
. That's easily overlooked. You can find the corrected HTML here: table10.html
这就是生成的PDF的样子: html_table_10 .pdf
This is what the resulting PDF looks like: html_table_10.pdf
您的代码和我的代码之间的一个主要区别是,我使用了iText / XML Worker 5.5.6(Java)并且您使用了iTextSharp / XML Worker(C#)。虽然Java iText和XML Worker与C#iTextSharp和XML Worker保持同步(因此应具有相同的行为),但底层XML解析器是不同的。
One major difference between your code and mine, is that I used iText / XML Worker 5.5.6 (Java) and you used iTextSharp / XML Worker (C#). While the Java iText and XML Worker are kept in sync with the C# iTextSharp and XML Worker (and should therefore have identical behavior), the underlying XML parser is different.
I知道我们决定表现得像浏览器(*),默默地允许一些糟糕的HTML构造失败,但我对Java XML解析的经验是抛出异常。也许C#XML解析没有。
I know that we decided to behave like browsers (*), silently allowing some bad HTML constructs to fail, but my experience with the Java XML parsing is that is throws exceptions. Maybe the C# XML parsing doesn't.
(*)我是iText Group的首席执行官,包括比利时,美国和新加坡的公司。
(*) I'm the CEO of the iText Group, consisting of companies in Belgium, the US and Singapore.
至于css resolver crying异常,我们将在下一个版本中将其删除:
As for the "css resolver crying" exception, we'll remove it in the next release: