简体   繁体   English

如何使用itext 7从HTML div生成PDF并将其保存到.net服务器上的文件夹中

[英]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.

相关问题 使用 IText 7 生成一个 pdf 文档,其中包含多个从 html 转换的页面 - Generate one pdf document with multiple pages converting from html using IText 7 使用 .net web api 在服务器端生成带有部分 HTML 内容的 PDF - Generate PDF with partial HTML content in server side with .net web api 如何在客户端(javascript)或服务器端的asp.net上将html页面的一部分保存到图像或pdf? - How to save part of an html page to an image or pdf either on client (javascript) or asp.net on the server side? 从包含 html 代码的 MySql 表中创建 pdf 文件,使用 iText7 和 ASP NET ZD7EFA19FBE242236 - Create pdf file from MySql table containing html code using iText7 and ASP NET C# 如何在ASP.NET MVC中应用脚本和样式后从HTML视图生成PDF - How to generate PDF from HTML view after scripts and styles applied in ASP.NET MVC 如何在 asp.net mvc5 中使用 html 生成 pdf - How to generate pdf using html in asp.net mvc5 如何从HTML生成PDF并使用CSS @页面编号(通过C#)? - How can I generate a PDF from HTML and use CSS @page numbering (via C#)? 使用iText for .NET从HTML输出图形 - output a graph from HTML with iText for .NET 如何使用 IText 7 从 PDF 中提取页面? - How to Extract pages from a PDF using IText 7? 如何使用 iText 从 PDF 中提取旋转图像 - How to extract rotated images from PDF with iText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM