基于Web的超大页面打印解决方案

如果需要打印的网页源码多达几M, 如此庞大的页面在下载渲染的时候会完全失去响应,直至崩溃。

其实我们可以在后台把html转换为pdf然后下载打印就可以解决这个问题了,选择一个好的html2pdf组件是个关键。

在此用到了expertpdf的两个dll,ephtmltopdf.dll和epmergepdf.dll

 

public class Html2Pdf

 

    {

        const string licenseKey = "Your License" ;

        List <string > fileList = new List <string >();

        PdfConverter pdfConverter = new PdfConverter ((int )(24 / 0.0352777777778), 0);

        public Html2Pdf()

        {

            pdfConverter.LicenseKey = licenseKey;

            pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize .A4;

            pdfConverter.PdfFooterOptions.ShowPageNumber = true ;

            pdfConverter.PdfDocumentOptions.LeftMargin = (int )(1.5 / 0.0352777777778);

            pdfConverter.PdfDocumentOptions.RightMargin = (int )(1.5 / 0.0352777777778);

            pdfConverter.PdfDocumentOptions.TopMargin = (int )(1.2 / 0.0352777777778);

            pdfConverter.PdfDocumentOptions.BottomMargin = (int )(1.2 / 0.0352777777778);

            pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = false ;

            pdfConverter.PdfDocumentOptions.FitWidth = true ;

            pdfConverter.PdfDocumentOptions.AutoSizePdfPage = true ;

            pdfConverter.ScriptsEnabled = false ;

            pdfConverter.ActiveXEnabled = false ;

            pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel .Best;

            pdfConverter.PdfDocumentOptions.CustomPdfPageSize = new SizeF ((int )(17 / 0.0352777777778), 0);//new SizeF((int)(17 * 72 / 2.54), 0);

        }

        /// <summary>

        /// Convert Html string to Pdf file.

        /// </summary>

        /// <param name="strHtml"> Html contents </param>

        /// <param name="strBaseUrl"> Just like http://www.xxx.com:80 </param>

        /// <param name="strTempLocation"> Temp folder to save pdf file for each employee </param>

        /// <returns> Return filePath which was saved </returns>

        public string ExportPDF(string strHtml, string strBaseUrl, string strTempLocation)

        {

            string fileName = GetFileName(strTempLocation);

            pdfConverter.SavePdfFromHtmlStringToFile(strHtml, fileName, strBaseUrl);

            fileList.Add(fileName);

            return fileName;

        }

        /// <summary>

        /// After called ExportPDF method,then merge each pdf files to one big file.

        /// each employee pdf file will be deleted, only remain merged file.

        /// </summary>

        /// <param name="strTempLocation"> Temp folder to save pdf file for one big file. </param>

        /// <returns> Return filePath which was saved </returns>

        public string MergePDF(string strTempLocation)

        {

             ExpertPdf.MergePdf.PDFMerge pdfMerge = new ExpertPdf.MergePdf.PDFMerge ();

            //pdfMerge.LicenseKey = licenseKey;

            foreach (string file in fileList)

            {

                pdfMerge.AppendPDFFile(file);

                File .Delete(file);

            }

            string fileName = GetFileName(strTempLocation);

            pdfMerge.SaveMergedPDFToFile(fileName);

            return fileName;

        }

        private string GetFileName(string strTempLocation)

        {

            return Path .Combine(strTempLocation, Guid .NewGuid().ToString() + ".pdf" );

        }

    }

比如我们打印Employee的信息,一次打印25个Employee,每个Employee都有大量的相关信息。

调用ExportPDF方法为每个Employee输出单独 一个pdf ,然后调用 MergePDF将所有的html合并到一个pdf。

用这种拆分然后合并的方式比25个Employee一下子生成快很多,而且不会导致服务器内存溢出,因为渲染一个html需要大量的内存。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值