简体   繁体   中英

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. 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. Would really appreciate any help.

Bellow is the code for the on-click button event I use to generate the 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

This is an example on how to use 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM