【问题标题】:Images disappeared in PDF made by Pechkin.SynchronizedPechkin.Synchronized 制作的 PDF 中的图像消失了
【发布时间】:2015-12-10 06:58:47
【问题描述】:

我编写了一些代码,用于使用 pechkin 和 pechkin.synchronized 从 HTML 制作 PDF。

第一次会很好用。

自第二次以来,图像在 pdf 中消失了。 其他 [html 到 pdf] 也是如此。第一个pdf是正确的。其他pdf不是。 在服务器第一次使用时,它会工作。

可能是因为 pechkin 不会在内存中卸载。我想。

我需要帮助。

System.IO.File.WriteAllText(textfilepath, html);
GlobalConfig gc = new GlobalConfig();

ObjectConfig oc = new ObjectConfig()
    .SetLoadImages(true)
    .SetZoomFactor(1.0)
    .SetPrintBackground(true)
    .SetScreenMediaType(true)
    .SetCreateExternalLinks(true)
    .SetAllowLocalContent(true)
    .SetPageUri(url);

IPechkin pechkin = new Pechkin.Synchronized.SynchronizedPechkin(gc);

pdf = pechkin.Convert(oc);

【问题讨论】:

    标签: c# html image pdf pechkin


    【解决方案1】:
    using NReco.ImageGenerator;
    using NReco.PdfGenerator;
    
    //아래 주석들을 잘 여닫으면 jpg로 저장할 수 있다.
      /*
      'C:\inetpub\wwwroot\...\bin\wkhtmltopdf.exe' 경로에 대한 액세스가 거부되었습니다.
      'C:\inetpub\wwwroot\...\bin\msvcp120.dll' 경로에 대한 액세스가 거부되었습니다. 
      'C:\inetpub\wwwroot\...\bin\msvcr120.dll' 경로에 대한 액세스가 거부되었습니다. 
    
      위 세 파일을 해당 경로에 넣어 주면 정상 작동한다.
      */
      string article_no = Request["article_no"];
      //string[] urlArray = Request["url"].Split('/');
      //string textfilename = Guid.NewGuid().ToString() + ".html";
      //string url = urlArray[0] + "//" + urlArray[2] + "/storage/print/" + textfilename;
      //string textfilepath = Server.MapPath("/storage/print/" + textfilename);
      string html = "<html>"+HttpUtility.UrlDecode(Request["html"])+"</html>";
      //string jpgpath = Server.MapPath("/storage/print/") + article_no + ".jpg";
      string pdfpath = Server.MapPath("/storage/print/") + article_no + ".pdf";
    
      try
      {
        //byte[] jpg;
        byte[] pdf;
        //if (System.IO.File.Exists(jpgpath))
        if (System.IO.File.Exists(pdfpath))
        {
          //jpg = System.IO.File.ReadAllBytes(jpgpath);
          pdf = System.IO.File.ReadAllBytes(pdfpath);
        }
        else
        {
          #region Transform the HTML into PDF
          //System.IO.File.WriteAllText(textfilepath, html);
    
          //jpg
          /*
          var htmlToImageConv = new HtmlToImageConverter();
          jpg = htmlToImageConv.GenerateImage(html, ImageFormat.Jpeg);
          //jpg = htmlToImageConv.GenerateImageFromFile(textfilepath, ImageFormat.Jpeg);
          */
    
      //pdf
      var htmlToPdf = new HtmlToPdfConverter();
          pdf = htmlToPdf.GeneratePdf(html);
          //htmlToPdf.GeneratePdfFromFile(url, null, pdfpath);
    
          System.IO.File.WriteAllBytes(pdfpath, pdf);
          //System.IO.File.Delete(textfilepath);
          #endregion
        }
    
        #region Return the pdf file
        Response.Clear();
    
        Response.ClearContent();
        Response.ClearHeaders();
    
        Response.ContentType = "application/pdf";
        //Response.AddHeader("Content-Disposition", string.Format("attachment;filename={1}.jpg; size={0}", jpg.Length, article_no));
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={1}.pdf; size={0}", pdf.Length, article_no));
        Response.BinaryWrite(pdf);
    
        Response.Flush();
        Response.End();
        #endregion
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 2021-08-28
      • 1970-01-01
      • 2015-03-22
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多