我正在尝试从我拥有的html模板生成pdf。 html模板有一个外部css文件,用于样式和格式设置。我能够生成pdf,但由于某种原因它从不使用css表。在过去的几天里,我一直在试图让这项工作失败。我唯一能找到的就是使用xmlworker。但我无法想象在我的代码中使用它。有人可以帮我解决这个问题。
protected void Page_Load(object sender, EventArgs e)
{
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
System.IO.FileStream output = System.IO.File.Open(@"C:\test\" + "MyPdf.pdf", FileMode.Create);
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
// Read in the contents of the Receipt.htm file...
string contents = File.ReadAllText(Server.MapPath("~/template/Receipt.html"));
// Replace the placeholders with the user-specified text
contents = contents.Replace("[JOBNUM]", "1234567890");
// Step 4: Parse the HTML string into a collection of elements...
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
// Close the Document - this saves the document contents to the output stream
document.Close();
}