html2pdf 网页转PDF

Gayhub 链接

问遍谷歌百度,依然没有好的方案.

打开Gayhub ,发现万赞JS(html2PDF,HTML2canvas) 效果也就那个XX样,一张糊里糊涂的img 冒充精美的PDF?

经过一天的苦思冥想,借助HiqPDF (估计Itext , spirePDF 等类似的都可以,思路还用这个就可以了),终于实现了目前来看最完美的方案 - -而且,贼简单你敢信?

先来个效果图 (= ̄ω ̄=)

GayHub首页转PDF去水印效果.png

看到了吗 ,页眉的黄色 水印精确删除 ,不用PS ,文字成段的在文件里 ,可以随意加工 ,图片的位置 , 边缘都很完美 ... 放大了看,还是那么清晰 - -我草 ,牛B!

简单版本,读文件 - -好处是无视网站定制,坏处当然就是非自动化啦(,,・ω・,,)

在你的后台放一个接收器,java,golang,python版本的 ,改天懒癌减轻了就补:

        public ActionResult getWOW()
        {
            FileStream fs = new FileStream(Server.MapPath("/App_Data/wow.txt"), FileMode.OpenOrCreate, FileAccess.Read);//路径
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            var htm = sr.ReadToEnd();
            sr.Close();
            fs.Close();
            // create the HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // set browser width
            htmlToPdfConverter.BrowserWidth = 1440;

            // set browser height if specified, otherwise use the default
            htmlToPdfConverter.BrowserHeight = htmlToPdfConverter.BrowserWidth * 2;

            // set HTML Load timeout
            //htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

            // set PDF page size and orientation
            //htmlToPdfConverter.Document.PageSize = new PdfPageSize((float)(width / 2.4), (float)(height / 2.4));
            htmlToPdfConverter.Document.PageSize = new PdfPageSize(htmlToPdfConverter.BrowserWidth, htmlToPdfConverter.BrowserHeight);
            htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;

            // set the PDF standard used by the document
            htmlToPdfConverter.Document.PdfStandard = PdfStandard.Pdf;//checkBoxPdfA.Checked ? PdfStandard.PdfA :

            // set PDF page margins
            htmlToPdfConverter.Document.Margins = new PdfMargins(0);

            // set whether to embed the true type font in PDF
            htmlToPdfConverter.Document.FontEmbedding = true;

            // set triggering mode; for WaitTime mode set the wait time before convert
            htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;

            // set header and footer
            //SetHeader(htmlToPdfConverter.Document);
            //SetFooter(htmlToPdfConverter.Document);

            // set the document security
            //htmlToPdfConverter.Document.Security.OpenPassword = textBoxOpenPassword.Text;
            htmlToPdfConverter.Document.Security.AllowPrinting = true;

            // set the permissions password too if an open password was set
            if (htmlToPdfConverter.Document.Security.OpenPassword != null && htmlToPdfConverter.Document.Security.OpenPassword != String.Empty)
                htmlToPdfConverter.Document.Security.PermissionsPassword = htmlToPdfConverter.Document.Security.OpenPassword + "_admin";

            //Cursor = Cursors.WaitCursor;

            // convert HTML to PDF
            string pdfFile = null;
            // convert URL to a PDF memory buffer
            //string url = formCollection["textBoxUrl"];
            byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htm, null);

            // send the PDF document to browser
            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "HtmlToPdf.pdf";

            return File(pdfBuffer, "application/octet-stream", "test.pdf");//最后一个双引号里面是回传的文件名,加个time值就可以避免重复了
        }

在浏览器按F12打开控制台(console)输入document.getElementsByTagName("html")[0].innerHTML

把得到的字符串拷贝到路径保存

然后把服务器跑起来,激活一下这个controller "http://localhost:3095/"

然后你就就会下载一个PDF,打开看看,激不激动?开不开心?

进阶版本- -前后端交互

  1. Controller:

        public ActionResult GetPDFfromHtmlCode(int width, int height, string htm)
        {
            htm = Server.UrlDecode(htm);
            //var path = Server.MapPath("/").Replace("\\", "/");
            var path = @"C:\Users\Public\Documents\DevExpress Demos 18.1\Components\ASP.NET\CS\ASPxCardViewDemos";
            htm = htm.Replace("/Content/",path+ "/Content/")
                .Replace("src=\"", "src=\""+path)
                //.Replace("
                    
                    

你可能感兴趣的:(html2pdf 网页转PDF)