locked
Rendering Image in PDF conversion RRS feed

  • Question

  • User-1994446809 posted

    I have a code that converts web page to PDF. How can image be rendered in this code that converts web page content to PDF?

    protected void Button2_Click(object sender, EventArgs e)
        {
            string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
    
    
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Panel1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
            PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
            cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
            IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
            var worker = new XMLWorker(pipeline, true);
            var xmlParse = new XMLParser(true, worker);
            pdfDoc.Open();
            xmlParse.Parse(sr);
            xmlParse.Flush();
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ";");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    
    Wednesday, October 28, 2020 9:53 AM

Answers

  • User-189459990 posted

    Did you want to print the image in exported pdf?

    Here is a simple demo you can refer to.<o:p></o:p>

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
    

    Please notice that you need to set the image url with full absolute path(contains “http://”), like<o:p></o:p>

    <img src="http://www.thedomain.com/images/test.png" /><br />

    Or,

    <img src="@System.Web.HttpContext.Current.Server.MapPath("~/images/test.png")" />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2020 9:23 AM

All replies

  • User-189459990 posted

    Did you want to print the image in exported pdf?

    Here is a simple demo you can refer to.<o:p></o:p>

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
    

    Please notice that you need to set the image url with full absolute path(contains “http://”), like<o:p></o:p>

    <img src="http://www.thedomain.com/images/test.png" /><br />

    Or,

    <img src="@System.Web.HttpContext.Current.Server.MapPath("~/images/test.png")" />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2020 9:23 AM
  • User-1994446809 posted

    Hi kyle0418,

    kyle0418

    Please notice that you need to set the image url with full absolute path(contains “http://”)

    I know about setting the full image url, but that is only when the image is in the image folder of the project, where the full path can be gotten. But the thing is, in my Web page the image will be uploaded on to the page; it is not going to be from the image folder. So the image url is unknown since the user will only upload it. In this case which url will be used please, or is there any other way to render image in PDF with full image path where image is uploaded from client/user side?

    Thursday, October 29, 2020 10:20 AM
  • User409696431 posted

    "So the image url is unknown since the user will only upload it. "

    Upload it to where?

    Thursday, October 29, 2020 6:04 PM
  • User-1994446809 posted

    KathyW

    Upload it to where?

    Upload it to an image control, using fileupload button

    Thursday, October 29, 2020 6:56 PM
  • User409696431 posted

    Fileupload - save it to a folder in your site, then display it.  What are you doing now?

    Friday, October 30, 2020 2:19 AM
  • User-1994446809 posted

    I thought that after uploading an image file to an image control and the image displays, then click on a button to convert to PDF, the image will be displayed in the PDF

    Friday, October 30, 2020 4:21 AM
  • User409696431 posted

    I don't know what you mean by "uploading a file to an image control".

    Save the file to a folder on your site.  Reference that file in the image control.   You'll have a URL to use for the PDF.

    If the image isn't supposed to hang around after the PDF is generated, delete it from your site folder after creating the PDF.

    Friday, October 30, 2020 7:17 AM
  • User-1994446809 posted

    Understood. Thanks

    Friday, October 30, 2020 9:21 AM