Pechkin documentation / tutorial

两盒软妹~` 提交于 2019-12-05 08:04:29

问题


I tried wkhtmltopdf which worked perfectly... But on the server I cant start a process so my solution would be this: https://github.com/gmanny/Pechkin

But I have no idea how to implement it in my project... Are there any samples I can just open and run?

I had a look at this example:

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

But I didn't make it work... It looks so easy but no idea how to implement it... But I see Gman has worked with it.

Thank you in advance... :)


回答1:


Here is the simplest example I could think of which also writes the output to a file.

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



回答2:


use this

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



回答3:


Another Example of use

A little more complete, because you can set the margins of page

            // 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: In this example, the background images are being loaded, but I advise you to encode the Image in the Html, otherwise I think it will not work!

Note that now you Have a pdfBuf as a Byte Array, you can save it to a file, or just load into a Memory Stream, like:

MemoryStream stream = new MemoryStream(pdfBuf);


来源:https://stackoverflow.com/questions/12121508/pechkin-documentation-tutorial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!