Ejemplo n.º 1
-1
        public static void CreatePdfFromHtml(string htmlFilePath)
        {
            var htmlFileInfo = new FileInfo(htmlFilePath);

            if (!htmlFileInfo.Exists)
            {
                throw new FileNotFoundException(htmlFileInfo.FullName);
            }

            Debug.Assert(htmlFileInfo.DirectoryName != null, "htmlFileInfo.DirectoryName != null");

            var tmpPdfFileInfo = new FileInfo(Path.Combine(htmlFileInfo.DirectoryName, "tmp.pdf"));
            var pdfOutFileInfo = new FileInfo(GetPdfEquivalentPath(htmlFileInfo.FullName));

            var gc = new GlobalConfig();

            gc.SetImageQuality(100);
            gc.SetOutputDpi(96);
            gc.SetPaperSize(1024, 1123);

            var oc = new ObjectConfig();

            oc.SetLoadImages(true);
            oc.SetAllowLocalContent(true);
            oc.SetPrintBackground(true);
            oc.SetZoomFactor(1.093);
            oc.SetPageUri(htmlFileInfo.FullName);

            if (tmpPdfFileInfo.Exists)
            {
                tmpPdfFileInfo.Delete();
            }

            IPechkin pechkin = new SynchronizedPechkin(gc);

            pechkin.Error += (converter, text) =>
                {
                    Console.Out.WriteLine("error " + text);
                };

            pechkin.Warning += (converter, text) =>
                {
                    Console.Out.WriteLine("warning " + text);
                };

            using (var file = File.OpenWrite(tmpPdfFileInfo.FullName))
            {
                var bytes = pechkin.Convert(oc);
                file.Write(bytes, 0, bytes.Length);
            }

            if (pdfOutFileInfo.Exists)
            {
                pdfOutFileInfo.Delete();
            }

            CreateDirectories(pdfOutFileInfo.DirectoryName);

            tmpPdfFileInfo.MoveTo(pdfOutFileInfo.FullName);
        }
Ejemplo n.º 2
-1
        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;
        }
Ejemplo n.º 3
-1
        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;
            }
        }