private void SaveHtmlAsPdf(string fName, string html) { GlobalConfig cfg = new GlobalConfig(); SimplePechkin sp = new SimplePechkin(cfg); ObjectConfig oc = new ObjectConfig(); oc.SetCreateExternalLinks(true) .SetFallbackEncoding(Encoding.ASCII) .SetLoadImages(true) .SetAllowLocalContent(true) .SetPrintBackground(true); byte[] pdfBuf = sp.Convert(oc, "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title></title></head><body>" + html + "</body></html>"); FileStream fs = new FileStream(fName, FileMode.Create, FileAccess.ReadWrite); foreach (var byteSymbol in pdfBuf) fs.WriteByte(byteSymbol); fs.Close(); }
private static byte[] ToPdf(string xml) { // create global configuration object GlobalConfig gc = new GlobalConfig(); // set it up using fluent notation gc.SetDocumentTitle("DeadFolder certificate").SetPaperSize(PaperKind.A4Rotated); // 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) .SetPrintBackground(true) .SetFallbackEncoding(Encoding.ASCII) .SetLoadImages(true) .SetIntelligentShrinking(true); //... etc // convert document return pechkin.Convert(oc, xml); }
private byte[] CreatePDF(HtmlDocument doc) { // Create global configuration object GlobalConfig gc = new GlobalConfig(); // Set it up using fluent notation gc.SetMargins(new Margins(50, 100, 0, 0)) .SetDocumentTitle("Request") .SetPaperSize(PaperKind.A4); // Create converter IPechkin pechkin = new SimplePechkin(gc); // Create document configuration object ObjectConfig oc = new ObjectConfig(); // And set it up using fluent notation oc.SetCreateExternalLinks(true) .SetFallbackEncoding(Encoding.ASCII) .SetZoomFactor(2) .SetIntelligentShrinking(true) .SetLoadImages(true); // Convert document return pechkin.Convert(oc, doc.DocumentNode.OuterHtml); }
public HttpResponseMessage PrintReceipt(string receiptId) { var path = HttpContext.Current.Server.MapPath("~/receiptTemplate.html"); var template = File.ReadAllText(path); var result = new HttpResponseMessage(HttpStatusCode.OK); var receipt = _dal.GetReceipt(Convert.ToInt32(receiptId)); var client = _dal.GetClient(receipt.ClientId); var receiptTemplate = template; receiptTemplate = receiptTemplate.Replace("{indexNumber}", receipt.IndexNumber.ToString()); receiptTemplate = receiptTemplate.Replace("{total}", receipt.TotalAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{vat}", receipt.VatAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{vatpercent}", receipt.VatPercent.ToString()); receiptTemplate = receiptTemplate.Replace("{net}", receipt.NetAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{firstName}", client.FirstName); receiptTemplate = receiptTemplate.Replace("{lastName}", client.LastName); receiptTemplate = receiptTemplate.Replace("{address}", client.Address); receiptTemplate = receiptTemplate.Replace("{jobTitle}", client.Title); receiptTemplate = receiptTemplate.Replace("{afm}", client.AFM); receiptTemplate = receiptTemplate.Replace("{doy}", client.DOY); receiptTemplate = receiptTemplate.Replace("{desciption}", receipt.ReceiptDescription); receiptTemplate = receiptTemplate.Replace("{date}", receipt.Date.ToString("dd-MM-yyyy")); var gc = new GlobalConfig(); gc.SetPaperSize(kind: PaperKind.A4); gc.SetDocumentTitle(client.Address.Replace(".", "_")); var margins = new Margins(30, 30, 30, 30); gc.SetMargins(margins); var pechkin = Factory.Create(gc); var objConfig = new ObjectConfig(); objConfig.SetLoadImages(true); objConfig.SetPrintBackground(true); objConfig.SetScreenMediaType(true); objConfig.SetCreateExternalLinks(true); //objConfig.Footer.SetRightText(footerlbl.Text).SetFontSize(6).SetFontName("Verdana"); var pdf = pechkin.Convert(objConfig, receiptTemplate); var stream = new MemoryStream(pdf); result.Headers.AcceptRanges.Add("bytes"); result.Content = new StreamContent(stream); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = string.Format("receipt_{0}_{1}.pdf", DateTime.Now.ToShortDateString(), receipt.IndexNumber); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; }
public bool SaveMultipleReceipts(int month, int year) { try { var receipts = _dal.SaveMultipleReceipts(month, year); var path = HttpContext.Current.Server.MapPath("~/receiptTemplate.html"); var template = File.ReadAllText(path); var zipPath = string.Format("{0}\\receipts_{1}_{2}.zip", HttpContext.Current.Server.MapPath("~/exports"), month, year); var sourceZip = ZipFile.Create(zipPath); sourceZip.BeginUpdate(); var gc = new GlobalConfig(); gc.SetPaperSize(kind: PaperKind.A4); var margins = new Margins(30, 30, 30, 30); gc.SetMargins(margins); var pechkin = Factory.Create(gc); var objConfig = new ObjectConfig(); objConfig.SetLoadImages(true); objConfig.SetPrintBackground(true); objConfig.SetScreenMediaType(true); objConfig.SetCreateExternalLinks(true); var fileList = new List<string>(); foreach (var r in receipts) { var receiptTemplate = template; receiptTemplate = receiptTemplate.Replace("{indexNumber}", r.Receipt.IndexNumber.ToString()); receiptTemplate = receiptTemplate.Replace("{total}", r.Receipt.TotalAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{vat}", r.Receipt.VatAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{vatpercent}", ConfigurationManager.AppSettings["DefaultVatPercent"]); receiptTemplate = receiptTemplate.Replace("{net}", r.Receipt.NetAmount.ToString()); receiptTemplate = receiptTemplate.Replace("{firstName}", r.Client.FirstName); receiptTemplate = receiptTemplate.Replace("{lastName}", r.Client.LastName); receiptTemplate = receiptTemplate.Replace("{address}", r.Client.Address); receiptTemplate = receiptTemplate.Replace("{jobTitle}", r.Client.Title); receiptTemplate = receiptTemplate.Replace("{afm}", r.Client.AFM); receiptTemplate = receiptTemplate.Replace("{doy}", r.Client.DOY); receiptTemplate = receiptTemplate.Replace("{desciption}", r.Receipt.ReceiptDescription); receiptTemplate = receiptTemplate.Replace("{date}", r.Receipt.Date.ToString("dd-MM-yyyy")); gc.SetDocumentTitle(r.Client.Address.Replace(".", "_")); var pdf = pechkin.Convert(objConfig, receiptTemplate); r.Client.Address = r.Client.Address.Replace(@"/", "-").Replace(@"\", "-"); r.Client.AdministrationOffice = r.Client.AdministrationOffice.Replace(@"/", "-").Replace(@"\", "-"); var filename = string.Format("{0}\\{1}_{2}{3}.pdf", HttpContext.Current.Server.MapPath("~/exports"), r.Client.IndexNumber, r.Client.Address, (!String.IsNullOrEmpty(r.Client.AdministrationOffice) ? "_" + r.Client.AdministrationOffice : "")); File.WriteAllBytes(filename, pdf); fileList.Add(filename); sourceZip.Add(new StaticDiskDataSource(filename), Path.GetFileName(filename), CompressionMethod.Deflated, true); } sourceZip.CommitUpdate(); sourceZip.Close(); foreach (var file in fileList) { File.Delete(file); } return true; } catch (Exception ex) { return false; } }