简体   繁体   中英

Html to PDF ITextSharp Image

I am converting my Html to PDF using ITextSharp, the problem arises when i try to insert image to HTML page but when the PDF is downloaded i cannot see the Image in it. I don't see any errors, code works fine but I cannot see the Image in the downloaded PDF. The path of PDF is right as it works in HTML PAGE, I even tried to change the dimensions of the Image & canvas using HTML but that didn't solve the problem.

Coding to convert HTML to PDF is:

public void DownloadAsPDF()
    {
        try
        {
            string case_id = Request.Form["case_id"];
            string strHtml = string.Empty;
            string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + case_id + ".pdf";


            string template = System.IO.File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));

Code to Insert Image Starts

            Base64ToImage().Save(Server.MapPath("\\files\\" + case_id + "stu.jpg"));

            Base64ToInsImage().Save(Server.MapPath("\\files\\" + case_id + "ins.jpg"));
            string facultysign = "/files/CS00022904stu.jpg";
            string stusign = "/files/CS00022904stu.jpg";
            template = template.Replace("[stusign]", facultysign);
            template = template.Replace("[facultysign]", stusign);

Code to Insert Image Ends

            CreatePDFFromHTMLFile(template, pdfFileName);
 }
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
    {
        try
        {
            object TargetFile = FileName;

            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;

            GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
            GeneratePDF.HtmlPdfPage first = builder.AddPage();
            first.AppendHtml(HtmlStream);
            byte[] file = builder.RenderPdf();
            System.IO.File.WriteAllBytes(TargetFile.ToString(), file);

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
            ModifiedFileName = TargetFile.ToString();
            ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

            iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
            reader.Close();
            if (System.IO.File.Exists(TargetFile.ToString()))
                System.IO.File.Delete(TargetFile.ToString());
            FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
            System.IO.File.Copy(ModifiedFileName, FinalFileName);
            if (System.IO.File.Exists(ModifiedFileName))
                System.IO.File.Delete(ModifiedFileName);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

*HMTL Coding to Insert Image *

 <p><strong>Faculty Signature: </strong></p>
   <img src='[stusign]' />

 <p><strong>Faculty Signature: </strong></p>
   <img src='[facultysign]' />

调整图像大小可以为我解决问题(即使调整画布大小也可以解决问题)。

后面代码中的pdfptablepdfpcell可用于定位PDF中的图像。

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