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. SAAS相关技术要点

    这篇文章本来是我们开发组内部用的一个小文档.因为我们公司以前没有做SAAS的经验,就成立了一个小组做一做这方面的技术前探,我是成员之一.这篇文档想从宏观的层面把开发一个SAAS应用所要用到的技术点稍微 ...

  2. Eclipse的设置小细节提高开发效率

    1. 自动联想功能增强 preference->java->Editor->Content Assist中, Auto activation triggers for java中默认 ...

  3. asp.net 类库中获取session c#类中获取session

    asp.net  类库中获取session c#类中获取session 1. 先引入命名空间 using System.Web; using System.Web.SessionState; 在使用H ...

  4. EditText的 焦点事件 setOnFocusChangeListener

    实现代码: //光标处在EditText时其内容消失 mInfo = (EditText)findViewById(R.id.old_password); //setOnFocusChangeList ...

  5. DragSortListView学习总结

    Drag-sort-listview 是一个支持拖拽排序和左右滑动删除功能的自定义ListView,重写了 TouchInterceptor类来提供更加优美的拖拽动画效果. DSLV主要特性: 完美的 ...

  6. Git的分支与合并

    在Git里面我们可以创建不同的分支,来进行调试.发布.维护等不同工作,而互不干扰.下面我们还是来创建一个试验仓库,看一下Git分支运作的台前幕后: $rm -rf test_branch_proj $ ...

  7. Host group 信息

  8. Tomcat禁止显示目录和文件列表

    Tomcat禁止显示目录和文件列表 打开   tomcat的安装目录/conf/web.xml 文件 <servlet> <servlet-name>default</s ...

  9. Hadoop 新 MapReduce 框架 Yarn 详解

    Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...

  10. Linux Shell编程(28)——进程替换

    进程替换与命令替换很相似. 命令替换把一个命令的结果赋给一个变量,例如 dir_contents=`ls -al`或xref=$. 进程替换则是把一个进程的输出回馈给另一个进程 (换句话说,它把一个命 ...