[英]Pechkin documentation / tutorial
我嘗試了運行完美的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已經使用了它。
先感謝您... :)
這是我能想到的最簡單的示例,該示例還將輸出寫入文件。
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);
// }
}
使用的另一個例子
更完整一些,因為您可以設置頁面的邊距
// 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);
附:在此示例中,正在加載背景圖像,但是我建議您將圖像編碼為Html ,否則我認為它將不起作用!
請注意,現在您有了一個pdfBuf作為字節數組,您可以將它保存到文件中,或僅加載到內存流中,例如:
MemoryStream stream = new MemoryStream(pdfBuf);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.