Пример #1
0
        public static Stream Html2Pdf(PrinterData printerData)
        {
            string htmlText = printerData.data.Trim();

            if (string.IsNullOrEmpty(htmlText))
            {
                throw new Exception("传入的html无内容:" + htmlText);
            }
            MemoryStream outputStream = new MemoryStream();       //实例化MemoryStream,用于存PDF

            byte[]       data = Encoding.UTF8.GetBytes(htmlText); //字串转成byte[]
            MemoryStream msInput = new MemoryStream(data);
            int          width = 0, height = 0;

            width  = (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.width);
            height = (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.height);
            if (!printerData.customSizeFlag)
            {
                PrintUtils.getPaperSize(printerData.paperName ?? "A4", ref width, ref height);
            }
            Rectangle pageSize = new Rectangle(width, height); //设置pdf模板大小

            if (printerData.direction == "2")                  //2:横向打印
            {
                pageSize = new Rectangle(height, width);
            }
            Document  doc    = new Document(pageSize); //要写PDF的文件 document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //指定文件预设开档时的缩放为100%
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);

            //开启Document文件
            doc.Open();

            //使用XMLWorkerHelper把Html parse到PDF档里
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
            //XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8);

            //将pdfDest设定的资料写到PDF档
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

            writer.SetOpenAction(action);
            doc.Close();
            msInput.Close();
            outputStream.Close();
            //回传PDF档案
            var bytes = outputStream.ToArray();

            var ret = Convert.ToBase64String(bytes);

            try
            {
                string strbase64 = ret;
                strbase64 = strbase64.Replace(' ', '+');
                return(new MemoryStream(Convert.FromBase64String(strbase64)));
            }
            catch (Exception ex)
            {
                throw new Exception("html转pdf文件失败:" + ex.Message);
            }
        }
Пример #2
0
        public static void Html2Pdf(string htmlText, string tempPdfPath, string paperName, string direction, Boolean customSizeFlag, int customWidth, int customHeight)
        {
            if (string.IsNullOrEmpty(htmlText))
            {
                throw new Exception("传入的html无内容:" + htmlText);
            }
            MemoryStream outputStream = new MemoryStream();       //实例化MemoryStream,用于存PDF

            byte[]       data = Encoding.UTF8.GetBytes(htmlText); //字串转成byte[]
            MemoryStream msInput = new MemoryStream(data);
            int          width = 0, height = 0;

            width  = (int)Millimeters2PixelsUtils.Millimeters2Pixels(customWidth);
            height = (int)Millimeters2PixelsUtils.Millimeters2Pixels(customHeight);
            if (!customSizeFlag)
            {
                paperName = paperName ?? "A4";
                PrintUtils.getPaperSize(paperName, ref width, ref height);
            }
            Rectangle pageSize = new Rectangle(width, height); //设置pdf模板大小

            if (direction == "2")                              //2:横向打印
            {
                pageSize = new Rectangle(height, width);
            }
            Document  doc    = new Document(pageSize); //要写PDF的文件 document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //指定文件预设开档时的缩放为100%
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);

            //开启Document文件
            doc.Open();

            //使用XMLWorkerHelper把Html parse到PDF档里
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
            //XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8);

            //将pdfDest设定的资料写到PDF档
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

            writer.SetOpenAction(action);
            doc.Close();
            msInput.Close();
            outputStream.Close();
            //回传PDF档案
            var bytes = outputStream.ToArray();

            var ret = Convert.ToBase64String(bytes);

            try
            {
                string strbase64 = ret;
                strbase64 = strbase64.Replace(' ', '+');
                System.IO.MemoryStream stream = new System.IO.MemoryStream(Convert.FromBase64String(strbase64));
                System.IO.FileStream   fs     = new System.IO.FileStream(tempPdfPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                byte[] b = stream.ToArray();
                //byte[] b = stream.GetBuffer();
                fs.Write(b, 0, b.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("html转pdf文件失败:" + ex.Message);
            }
        }