C# 佩奇金文献/教程

C# 佩奇金文献/教程,c#,html,pdf,pechkin,C#,Html,Pdf,Pechkin,我试过wkhtmltopdf,效果非常好。。。但在服务器上,我无法启动进程,因此我的解决方案是: 但我不知道如何在我的项目中实现它。。。有什么样品我可以打开并运行吗 我看了一下这个例子: byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("<html><body><h1>Hello world!</h1></body></html>"); // cr

我试过wkhtmltopdf,效果非常好。。。但在服务器上,我无法启动进程,因此我的解决方案是:

但我不知道如何在我的项目中实现它。。。有什么样品我可以打开并运行吗

我看了一下这个例子:

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);
byte[]pdfBuf=new-SimplePechkin(new-GlobalConfig()).Convert(“Hello-world!”);
//创建全局配置对象
GlobalConfig gc=新的GlobalConfig();
//使用fluent符号设置它
总承包商设定利润(新利润(300100150100))
.SetDocumentTitle(“测试文件”)
.SetPaperSize(PaperKind.Letter);
//... 等
//创建转换器
IPechkin-pechkin=新的同步pechkin(gc);
//订阅活动
pechkin.Begin+=OnBegin;
pechkin.Error+=OnError;
pechkin.Warning+=OnWarning;
pechkin.PhaseChanged+=开相;
pechkin.ProgressChanged+=OnProgress;
pechkin.Finished+=未完成;
//创建文档配置对象
ObjectConfig oc=新的ObjectConfig();
//也可以用流畅的符号来设置
oc.SetCreateExternalLinks(错误)
.SetFallbackEncoding(Encoding.ASCII)
.SetLoadImages(假);
.SetPageUri(“http://google.com");
//... 等
//转换文件
字节[]pdfBuf=pechkin.Convert(oc);
但我没有让它工作。。。它看起来很简单,但不知道如何实现它。。。 但我看到Gman已经和它合作过了


提前谢谢你……:)

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

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

  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);
//            }
        }
private void CreatePdfPechkin(字符串htmlString,字符串文件名)
{
//将HTML转换为PDF
var pechkin=Factory.Create(新的GlobalConfig()
.SetMargins(新的保证金(100,50,100,100))
.SetDocumentTitle(“测试文件”)
.SetPaperSize(PaperKind.A4)
.SetCopyCount(1)
//.SetPaperOrientation(真)
//.SetOutputFile(“F:/Personal/test.pdf”)
);
ObjectConfig oc=新的ObjectConfig();
oc.Footer.SetLeftText(“[page]”);
oc.Footer.SetTexts(“[page]”、“[date]”、“[time]”);
oc.Header.SetCenterText(“测试头TEST1”);
oc.Header.SetHtmlContent(“测试标题V2”);
oc.SetAllowLocalContent(真);
////创建转换器
//IPechkin IPechkin=新同步的pechkin(pechkin);
//使用fluent符号设置它
var pdf=pechkin.Convert(新的ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(真)
.SetScreenMediaType(真)
.SetCreateExternalLinks(true),htmlString);
//返回PDF文件
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType=“application/pdf”;
AddHeader(“Content Disposition”,string.Format(“attachment;filename=test.pdf;size={0}”,pdf.Length”);
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
//字节[]pdf=新的Pechkin.Synchronized.SynchronizedPechkin(
//新的Pechkin.GlobalConfig()).Convert(
//新的Pechkin.ObjectConfig()
//.SetLoadImages(真)
//.SetPrintBackground(真)
//.SetScreenMediaType(真)
//.SetCreateExternalLinks(true),htmlString);
//使用(FileStream file=System.IO.file.Create(@“F:\Pankaj WorkSpace\”+文件名))
//            {
//file.Write(pdf,0,pdf.Length);
//            }
}

另一个使用示例

更完整一点,因为您可以设置页面的边距

            // 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);

谢谢,我在写了这篇文章几个小时后才发现。。。我很匆忙我需要它尽快工作。。。只是对代码的一个建议。。。使用Syncronized Pechin,它也可以在web服务器上工作。。。这是一个神奇的库,它可以正常工作…与itextsharp wich相比,我浪费了很多时间想知道为什么我不能让它正常工作,因为LoadStyle方法不起作用。。。顺便说一句,它的pechkin不是pushkin…:我不确定oc.Header.SetHtmlContent是否能与HTML一起工作,因为它需要一个url。只是尝试了一个页脚,它不工作