http://code.google.com/p/wkhtmltopdf/downloads/list下载安装程序。

1.添加引用

 using System.Diagnostics;

添加引用

2.方法

     /// <summary>
/// 把对应的网页转化成Pdf文件
/// </summary>
/// <param name="Url">网页网址</param>
/// <param name="Path"></param>
/// <returns>成功返回true失败返回false</returns>
public static bool HtmlTOPdf(string Url, string Path)
{
if (!string.IsNullOrEmpty(Url)&&!string.IsNullOrEmpty(Path))
{
try
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = "C:\\WINDOWS\\system32";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string cmd = "C:/Users/PC/Desktop/wkhtmltopdf/wkhtmltopdf.exe" + " " + Url + " " + Path + " ";
p.StandardInput.WriteLine(cmd);
p.WaitForExit();
p.Close();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return false;
}
}

方法

3.调用

  Html2Pdf.HtmlToPdf("http://www.cnblogs.com/", "d:/cnblogs.pdf");

调用

出现的问题:非utf-8编码的网页转换会出现乱码。有人做过测试:http://aiilive.blog.51cto.com/1925756/1340243

在网上找了一些其他的转换方法:

http://www.html-to-pdf.net/ 也不支持非utf-8的网页转换,测试网站 http://www.51cto.com/ 转换乱码

http://www.winnovative-software.com/ 测试成功

下面是代码:

需要添加wnvhtmltopdf.dll

 using Winnovative;

添加引用

         //create a PDF document
Document document = new Document(); //optional settings for the PDF document like margins, compression level,
//security options, viewer preferences, document information, etc
document.CompressionLevel = PdfCompressionLevel.Normal;
document.Margins = new Margins(, , , );
//document.Security.CanPrint = true;
//document.Security.UserPassword = "";
document.ViewerPreferences.HideToolbar = false; // set if the images are compressed in PDF with JPEG to reduce the PDF document size
document.JpegCompressionEnabled = true; //Add a first page to the document. The next pages will inherit the settings from this page
PdfPage page = document.Pages.AddNewPage(PdfPageSize.A4, new Margins(, , , ), PdfPageOrientation.Portrait); // the code below can be used to create a page with default settings A4, document margins inherited, portrait orientation
//PdfPage page = document.Pages.AddNewPage(); // add a font to the document that can be used for the texts elements
PdfFont font = document.Fonts.Add(new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), ,
System.Drawing.GraphicsUnit.Point)); // the result of adding an element to a PDF page
AddElementResult addResult; // Get the specified location and size of the rendered content
// A negative value for width and height means to auto determine
// The auto determined width is the available width in the PDF page
// and the auto determined height is the height necessary to render all the content
float xLocation = ;
float yLocation = ;
float width = ;
float height = ; // convert HTML to PDF
HtmlToPdfElement htmlToPdfElement; // convert a URL to PDF
string urlToConvert = "http://www.51cto.com/"; htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height, urlToConvert); //optional settings for the HTML to PDF converter
htmlToPdfElement.FitWidth = true;//合适的宽度
htmlToPdfElement.EmbedFonts = false;//嵌入字体呈现PDF文档中真正的类型
htmlToPdfElement.LiveUrlsEnabled = false;//网页链接是否可用
htmlToPdfElement.JavaScriptEnabled = true;//在转换期间是否启用JavaScript和其他客户端脚本
htmlToPdfElement.PdfBookmarkOptions.HtmlElementSelectors = null;//Bookmark H1 and H2 HTML tags // add theHTML to PDF converter element to page
addResult = page.AddElement(htmlToPdfElement); try
{
// get the PDF document bytes
byte[] pdfBytes = document.Save(); // send the generated PDF document to client browser // get the object representing the HTTP response to browser
HttpResponse httpResponse = HttpContext.Current.Response; // add the Content-Type and Content-Disposition HTTP headers
httpResponse.AddHeader("Content-Type", "application/pdf");
httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename=51cto.pdf; size={0}", pdfBytes.Length.ToString())); // write the PDF document bytes as attachment to HTTP response
httpResponse.BinaryWrite(pdfBytes); // Note: it is important to end the response, otherwise the ASP.NET
// web page will render its content to PDF document stream
httpResponse.End();
}
finally
{
// close the PDF document to release the resources
document.Close();
}

用wnvhtmltopdf把Html转换成pdf

Html网页生成Pdf的更多相关文章

  1. pypdf2:下载Americanlife网页生成pdf合并pdf并添加书签

    初步熟悉 安装 pip install pypdf2 合并并添加书签 #!/usr/bin/env python3.5 # -*- coding: utf-8 -*- # @Time : 2019/1 ...

  2. 实践指南-网页生成PDF

    一.背景 开发工作中,需要实现网页生成 PDF 的功能,生成的 PDF 需上传至服务端,将 PDF 地址作为参数请求外部接口,这个转换过程及转换后的 PDF 不需要在前端展示给用户. 二.技术选型 该 ...

  3. tcpdf 将网页生成pdf

    需求:需要将HTML页面生成PDF文档 开发语言:PHP 使用TCPDF第三方类库进行生成,下载地址:http://sourceforge.net/projects/tcpdf/ 核心代码: publ ...

  4. Freemarker + iTextRender 实现根据模板网页生成PDF

    #0 背景 工作需要实现导出PDF的功能,在进行简单调研后,我决定采用Freemarker + iTextRender进行实现. 基本思路如下: Freemarker实现根据动态数据渲染出需要导出的H ...

  5. 利用SelectPdf插件将网页生成PDF

    简介 适用于.NET Framework和.NET Core的HTML至PDF转换器 SelectPdf提供的在线html到pdf转换器使用.NET的Select.Pdf库中的html到pdf转换器. ...

  6. wkhtmltopdf 将网页生成pdf文件

    先安装依赖 yum install fontconfig libXrender libXext xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi freetype l ...

  7. PHP 生成PDF

    一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...

  8. 采用TuesPechkin生成Pdf

    1.需求 前段时间有个需求,要求把网页生成pdf,找了各种插件,才决定使用这个TuesPechkin,这个是后台采用C#代码进行生成 2.做法 我要做的是一个比较简单的页面,采用MVC绑定,数据动态加 ...

  9. 动态将ASPX生成HTML网页并将网页导出PDF

    1.首先要找到wnvhtmlconvert.dll这个文件,并引入项目中. 2.Server.Execute("pos.aspx?id=" + ids); 执行相应的aspx网页 ...

随机推荐

  1. RedHat 6.5 离线安装 apache2.4.23

    第一部分:安装gcc等 rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm rpm -ivh cpp- ...

  2. Energy Minimization

    zoj2539:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2539 题意:公式第一项只要当xi=0时才会有作用,第二项只 ...

  3. 转-[Python 学习]2.5版yield之学习心得

    在 shhgs 发布了关于< Py 2.5 what’s new 之 yield>之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一 ...

  4. 【HDU2825】Wireless Password (AC自动机+状压DP)

    Wireless Password Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u De ...

  5. Mysql 8个小时连接断开问题解析

    wait_timeout — 指的是mysql在关闭一个非交互的连接之前所要等待的秒数,其取值范围为1-2147483(Windows),1-31536000(linux),默认值28800. int ...

  6. 转 -- MVC+EF easyui dataGrid 动态加载分页表格

    首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...

  7. 【转】LaTeX 符号命令大全

    函数.符号及特殊字符 声调 语法 效果 语法 效果 语法 效果 \bar{x} \acute{\eta} \check{\alpha} \grave{\eta} \breve{a} \ddot{y} ...

  8. 【转】OpenGL相关函数库介绍

    原文:http://blog.chinaunix.net/uid-20638550-id-1909182.html OpenGL 函数库相关的API有核心库(gl).实用库(glu).辅助库(aux) ...

  9. Codeforces 350B Resort

    题目链接:http://codeforces.com/problemset/problem/350/B 一开始想复杂了,建了张图,结果效率太低T了.其实用数组存可以了,结果发现的时候快没时间了,修改好 ...

  10. Servlet实现表单提交(MyEclipse10,Tomcat7.0,JDK1.7,)——Java Web练习(一)

    1.MyEclipse|File|New|Project|Web Project    填写Project Name:exServlet,点选Java EE 6.0(配套Tomcat7.0) 2.代码 ...