写入pdf支持html,iTextSharp-将HTML写入pdf文档

I've tried my damnedest to get this to work, but all info on the web regarding iTextSharp seems to be out dated. (I'm using version 5.5.3)

I have a simple string variable containing the HTML that I want rendered to a PDF.

There are a few examples using XMLWorker / XMLWorkerHelper, but these classes no longer seem to be part of iTextSharp. HTMLWorker is still available, but the moans about being obsolete. (And also doesn't work)

This is what I have so far:

public byte[] RenderPdf()

{

MemoryStream file = new MemoryStream();

Document document = new Document(this.PageSize);

PdfWriter writer = PdfWriter.GetInstance(document, file);

string HTML = GetHTMLFromActionResult();

document.Open();

//Missing code here to inject HTML into document variable.

document.Close();

return file.ToArray();

}

Extra info:

This is a C# MVC5 Web Application running on .Net Framework 4.5.2

Talk1:

Whilst not a solution to this problem when I was working with this library I found the following book extremely useful - amazon.co.uk/iText-Action-Creating-Manipulating-PDF/dp/…. It's for the JAVA version but most of the code samples are very easily ported.

Solutions1

I see two separate questions in your post:

(1) Where is XML Worker for iTextSharp?

If you want to use XML Worker, you need an extra DLL. I'm not sure where you usually get iTextSharp, but if you go to SourceForge, you can clearly see both projects: http://sourceforge.net/projects/itextsharp/files/

Start by downloading itextsharp as well as xmlworker and make sure the version numbers correspond.

(2) Where can I find the documentation for XML Worker?

The last time I updated the examples was as long ago as last weekend, so it's not fair to say that they are outdated. Maybe you didn't search the official web site: http://itextpdf.com/sandbox/xmlworker

I recently added some examples in answer to the following questions:

Sure, the examples are iText examples in Java, but if you browse the examples, you'll find a link to the corresponding question on StackOverflow and many of those questions are about iTextSharp in Java. It's fairly easy to port a Java example to C#.

Talk1:

I'm honored to have you reply to my query Bruno! I got iTextSharp of Nuget, I assumed that it would pull down everything I needed. I did not pull down the XMLWorker. Thanks for your response, I was not aware that it was a separate assembly. I got the worker off Nuget too now. I should be able to figure rest from here.

Talk2:

Hi Bruno. The XMLWorker object complains about there being more than one head tag. I'm assuming this is because I am passing a full HTML page to the parser. Is there a way to not have the document object include its own html/head/body tags?

Talk3:

The document doesn't have its own html/head/body tags. It just looks at the HTML you are passing to the parsers. Does that HTML have two head tags?

Talk4:

There was a malformed meta tag which made the parser think that there was something wrong with the head tag. The PDF now gets generated, but none of my controls are being rendered, only labels. It seems that iText is ignoring all tags. Note: I'm using bootstrap 3.0, I have not yet figured out how to include the CSS, not sure if that is part of the cause.

Talk5:

Indeed: forms in PDF are technically very different from forms in HTML (for instance: in PDF, you can only have one form tag; in HTML you can have several), hence XML Worker does not support them.

Solutions2

You need to call writer.close().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值