Ejemplo n.º 1
0
        /// <summary>
        /// 生成PDF
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="widthMillimeters"></param>
        /// <param name="heightMillimeters"></param>
        /// <param name="IsThermalPrinter"></param>
        internal byte[] GetPDF(string htmlText, double widthMillimeters, ref double heightMillimeters, bool IsThermalPrinter, PaperMargins margins)
        {
            int    dpi  = GetDPI();
            double zoom = 1 * (GetWindowsScaling() / 100.0);

            OpenHtmlToPdf.PaperSize size = null;


            if (IsThermalPrinter)
            {
                size = new OpenHtmlToPdf.PaperSize(Length.Millimeters(widthMillimeters), Length.Millimeters(10));
            }
            else
            {
                size = new OpenHtmlToPdf.PaperSize(Length.Millimeters(widthMillimeters), Length.Millimeters(heightMillimeters));
            }

            IPdfDocument doc = Pdf.From(htmlText).EncodedWith("utf-8");

            byte[] finalPDF = null;
            finalPDF = doc.WithResolution(dpi).WithObjectSetting("load.zoomFactor", zoom.ToString()).WithObjectSetting("web.enableIntelligentShrinking", "false").WithMargins(margins).OfSize(size).Content();


            var document      = PdfReader.Open(new MemoryStream(finalPDF), PdfDocumentOpenMode.InformationOnly);
            int numberOfPages = document.PageCount;


            if (numberOfPages > 1 && IsThermalPrinter)
            {
                heightMillimeters = 10 * numberOfPages;
                finalPDF          = doc.WithResolution(dpi).WithObjectSetting("load.zoomFactor", zoom.ToString()).WithObjectSetting("web.enableIntelligentShrinking", "false").WithMargins(margins).OfSize(new OpenHtmlToPdf.PaperSize(Length.Millimeters(widthMillimeters), Length.Millimeters(heightMillimeters))).Content();
            }


            //File.WriteAllText("y:\\test1111.html", htmlText);
            //using (FileStream fs = new FileStream("y:\\test1111.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            //{
            //    fs.Write(finalPDF, 0, finalPDF.Length);
            //}


            return(finalPDF);
        }
Ejemplo n.º 2
0
 public static IPdfDocument OfSize(this IPdfDocument pdfDocument, PaperSize paperSize)
 {
     return pdfDocument
         .WithGlobalSetting("size.width", paperSize.Width)
         .WithGlobalSetting("size.height", paperSize.Height);
 }