Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public ActionResult DownloadPdf2()
        {
            string url = "http://www.baidu.com/";

            using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
            {
                ObjectConfig oc = new ObjectConfig();
                oc.SetPrintBackground(true);
                oc.SetLoadImages(true);
                oc.SetScreenMediaType(true);
                oc.SetPageUri(url);
                byte[] pdf = pechkin.Convert(oc);
                return(File(pdf, "application/pdf", "ExportPdf.pdf"));
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="htmlPath">for saving by pechkin,this parameter can be set with local path or url</param>
        /// <param name="pdfPath"></param>
        /// <returns></returns>
        public bool Save(string htmlPath, string pdfPath)
        {
            lock (this)
            {
                try
                {
                    config.SetPageUri(htmlPath);
                    Utility.LocalIOHelper.CheckDirectoryExist(pdfPath);
                    byte[] buf = null;
                    buf = pechkinTool.Convert(config);
                    if (buf == null)
                    {
                        return(false);
                    }

                    File.WriteAllBytes(pdfPath, buf);
                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
        }