【问题标题】:Pechkin documentation / tutorialPechkin 文档/教程
【发布时间】:2012-08-20 17:34:59
【问题描述】:

我尝试了完美运行的 wkhtmltopdf...但是在服务器上我无法启动进程,所以我的解决方案是:https://github.com/gmanny/Pechkin

但我不知道如何在我的项目中实现它...有没有我可以打开并运行的示例?

我看过这个例子:

byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("<html><body><h1>Hello world!</h1></body></html>");

// create global configuration object
GlobalConfig gc = new GlobalConfig();

// set it up using fluent notation
gc.SetMargins(new Margins(300, 100, 150, 100))
  .SetDocumentTitle("Test document")
  .SetPaperSize(PaperKind.Letter);
//... etc

// create converter
IPechkin pechkin = new SynchronizedPechkin(gc);

// subscribe to events
pechkin.Begin += OnBegin;
pechkin.Error += OnError;
pechkin.Warning += OnWarning;
pechkin.PhaseChanged += OnPhase;
pechkin.ProgressChanged += OnProgress;
pechkin.Finished += OnFinished;

// create document configuration object
ObjectConfig oc = new ObjectConfig();

// and set it up using fluent notation too
oc.SetCreateExternalLinks(false)
  .SetFallbackEncoding(Encoding.ASCII)
  .SetLoadImages(false);
  .SetPageUri("http://google.com");
//... etc

// convert document
byte[] pdfBuf = pechkin.Convert(oc);

但我没有让它工作......看起来很容易但不知道如何实现它...... 但我看到 Gman 已经使用它了。

提前谢谢你... :)

【问题讨论】:

    标签: c# html pdf pechkin


    【解决方案1】:

    另一个使用示例

    稍微完整一点,因为你可以设置页面的边距

                // create global configuration object
            GlobalConfig gc = new GlobalConfig();
    
            // set it up using fluent notation
            gc.SetMargins(new Margins(10, 10, 10, 10))
              .SetDocumentTitle("Test document")
              .SetPaperSize(PaperKind.Letter);
    
            //... etc
    
            // create converter
            IPechkin pechkin = new SynchronizedPechkin(gc);
    
            // create document configuration object
            ObjectConfig oc = new ObjectConfig();
    
            // and set it up using fluent notation too
            oc.SetCreateExternalLinks(false)
    
              .SetLoadImages(false)
              .SetPrintBackground(true)
              .SetLoadImages(true);
            //... etc
    
            // convert document
            byte[] pdfBuf = pechkin.Convert(oc, html);
    

    Ps:在这个例子中,背景图片正在加载,但我建议你将图片编码为Html,否则我认为它不起作用!

    请注意,现在您有一个 pdfBuf 作为字节数组,您可以将其保存到文件中,或者只是加载到内存流中,例如:

    MemoryStream stream = new MemoryStream(pdfBuf);
    

    【讨论】:

      【解决方案2】:

      使用这个

        private void CreatePdfPechkin(string htmlString, string fileName)
              {
                  //Transform the HTML into PDF
                  var pechkin = Factory.Create(new GlobalConfig()
                  .SetMargins(new Margins(100, 50, 100, 100))
                    .SetDocumentTitle("Test document")
                    .SetPaperSize(PaperKind.A4)
                     .SetCopyCount(1)
                                 //.SetPaperOrientation(true)
                                // .SetOutputFile("F:/Personal/test.pdf")
      
                   );
                  ObjectConfig oc = new ObjectConfig();
                  oc.Footer.SetLeftText("[page]");
                  oc.Footer.SetTexts("[page]", "[date]", "[time]");
                  oc.Header.SetCenterText("TEST HEADER TEST1");
                  oc.Header.SetHtmlContent("<h1>TEST HEADER V2</h1>");
                  oc.SetAllowLocalContent(true);
                 //// create converter
                  //IPechkin ipechkin = new SynchronizedPechkin(pechkin);
      
                  // set it up using fluent notation
                  var pdf = pechkin.Convert(new ObjectConfig()
                              .SetLoadImages(true).SetZoomFactor(1.5)
                              .SetPrintBackground(true)
                              .SetScreenMediaType(true)
                              .SetCreateExternalLinks(true), htmlString);
      
                 //Return the PDF file
                  Response.Clear();
                  Response.ClearContent();
                  Response.ClearHeaders();
                  Response.ContentType = "application/pdf";
                  Response.AddHeader("Content-Disposition", string.Format("attachment;filename=test.pdf; size={0}", pdf.Length));
                  Response.BinaryWrite(pdf);
                  Response.Flush();
                  Response.End();
      //            byte[] pdf = new Pechkin.Synchronized.SynchronizedPechkin(
      //new Pechkin.GlobalConfig()).Convert(
      //    new Pechkin.ObjectConfig()
      //   .SetLoadImages(true)
      //   .SetPrintBackground(true)
      //   .SetScreenMediaType(true)
      //   .SetCreateExternalLinks(true), htmlString);
      //            using (FileStream file = System.IO.File.Create(@"F:\Pankaj WorkSpace\"+  fileName))
      //            {
      //                file.Write(pdf, 0, pdf.Length);
      //            }
              }
      

      【讨论】:

      • 我不确定 oc.Header.SetHtmlContent 是否适用于 HTML,因为它需要一个 url。刚试了一个页脚,它不起作用
      【解决方案3】:

      这是我能想到的最简单的例子,它也将输出写入文件。

      byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert(html);
      File.WriteAllBytes(path, pdfBuf);
      

      【讨论】:

      • 谢谢我在写完这个帖子几个小时后发现...我很着急我需要它尽快工作...对代码只有一个建议...改用 Syncronized Pechin 它会也可以在 Web 服务器上工作......这是一个很棒的库,它只是工作......与 itextsharp 相比,我浪费了很多时间想知道为什么我不能让它工作是因为 LoadStyle 方法不起作用......顺便说一下它的 pechkin不是普希金……:P
      猜你喜欢
      • 2011-07-11
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      相关资源
      最近更新 更多