2012-08-25 96 views
-1

我試圖wkhtmltopdf它完美地工作......但在服務器上我不能啓動一個進程,所以我的解決辦法是這樣的:https://github.com/gmanny/PechkinPechkin文檔/教程

但我不知道如何實現它在我的項目。 ..有沒有我可以打開並運行的樣本?

我看看這個例子:

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有與它合作。

預先感謝您... :)

回答

5

這是我能想到的最簡單的例子也寫輸出到一個文件中。

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

感謝我發現幾個小時,我寫了這個之後線程...我很着急,我需要它儘快工作...只是一個建議的代碼...使用Syncronized Pechin,而不是它也可以在Web服務器上工作...這是驚人的庫,它只是工作.. .compared到itextsharp至極我失去了很多時間想知道爲什麼我不能使它的工作是因爲LoadStyle方法只是沒有工作......並btw它的pechkin不pushkin ...:P – user1518729

0

使用本

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

我不確定oc.Header.SetHtmlContent將使用HTML,因爲它期待着一個URL。剛試過它的腳註,它不起作用 – Manny42

0

使用的另一個例子

一點更加完整,因爲你可以設置頁邊距

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