[英]How to use itext 7 to generate a PDF from an HTML div and save it to a folder on the server in .net
I'm trying to create a CV builder that saves the CV edited by the user to a folder in my project for further processing of sending it through email, I have reached as far as using itext to create a PDF of an HTML div, but has no CSS or any of the text values I have returned from my database. 我正在尝试创建一个CV构建器,将用户编辑的CV保存到我项目中的文件夹中,以进行进一步的处理,以通过电子邮件发送它,我已经达到了使用itext来创建HTML div的PDF的目的,但是没有CSS或我从数据库返回的任何文本值。 Through some research i find that my problem could be solved by using itext 7 and an add-on pdfHTML but can not find any proper examples of how to use it with my ASP.NET code.
通过一些研究,我发现可以通过使用itext 7和附加的pdfHTML来解决我的问题,但是找不到如何将其与ASP.NET代码一起使用的适当示例。 Would really appreciate any help.
非常感谢您的帮助。
Bellow is the code for the on-click button event I use to generate the PDF 波纹管是我用来生成PDF的单击按钮事件的代码
protected void ButtonDownload_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
contentdiv.RenderControl(hw); //convert the div to PDF
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
string filename = base.Server.MapPath("~/PDF/" + "UserCV.pdf");
HttpContext.Current.Request.SaveAs(filename, false);
Response.End();
}
This picture shows the pdf result i get when i click the download button And this is html page it is trying to convert The text bellow the headings on the HTML page are Labels whose values are being set by retrieving values form a database 这张图片显示了我单击下载按钮时得到的pdf结果 ,这是html页面,它正在尝试转换 。HTML页面标题下方的文本是Labels,其值是通过从数据库中检索值来设置的
This is an example on how to use pdfHTML
这是有关如何使用
pdfHTML
This example is quite extensive, as it also sets document properties, and registers a custom Font. 该示例相当广泛,因为它还设置文档属性并注册自定义字体。
public void createPdf(String src, String dest, String resources) throws IOException {
try {
FileOutputStream outputStream = new FileOutputStream(dest);
WriterProperties writerProperties = new WriterProperties();
//Add metadata
writerProperties.addXmpMetadata();
PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
pdfDoc.getCatalog().setLang(new PdfString("en-US"));
//Set the document to be tagged
pdfDoc.setTagged();
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
//Set meta tags
PdfDocumentInfo pdfMetaData = pdfDoc.getDocumentInfo();
pdfMetaData.setAuthor("Joris Schellekens");
pdfMetaData.addCreationDate();
pdfMetaData.getProducer();
pdfMetaData.setCreator("JS");
pdfMetaData.setKeywords("example, accessibility");
pdfMetaData.setSubject("PDF accessibility");
//Title is derived from html
// pdf conversion
ConverterProperties props = new ConverterProperties();
FontProvider fp = new FontProvider();
fp.addStandardPdfFonts();
fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources
props.setFontProvider(fp);
props.setBaseUri(resources);
//Setup custom tagworker factory for better tagging of headers
DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
props.setTagWorkerFactory(tagWorkerFactory);
HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
pdfDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
The most relevant line here is 这里最相关的行是
HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
Which essentially tells pdfHTML
to perform the conversion of the inputstream (specified by src), put the content in pdfDoc
and use the given ConverterProperties
(specified by props
). 从本质
pdfHTML
,这告诉pdfHTML
执行inputstream的转换(由src指定),将内容放入pdfDoc
并使用给定的ConverterProperties
(由props
指定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.