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. Fail2ban用来作DDOS防守工具,不知够不够份量

    http://www.serversyntax.com/2012/12/how-to-secure-centos-server-ssh-fail2ban-ddos-deflate.html http: ...

  2. oracle的存储过程语法(转)

    1.ORA-00942: table or view does not exist 指的你要操作的表尚未存在,需要先create出来先. 2.ORA-00922: missing or invalid ...

  3. BZOJ1610: [Usaco2008 Feb]Line连线游戏

    1610: [Usaco2008 Feb]Line连线游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1301  Solved: 571[Submit ...

  4. css3图片墙

    css相关知识: 1. 使用box-shadow设置图片阴影,为照片加上阴影 eg: box-shadow: 0 0 5px 3px #abc 2. 使用tansform-origin定义变形原点 e ...

  5. Microsoft JScript 运行时错误: Automation 服务器不能创建对象

           var WshShell = new ActiveXObject('WScript.Shell')         WshShell.SendKeys( '{F11}');   问题: ...

  6. 【狼】openGL 光照的学习

    小狼学习原创,欢迎批评指正 http://www.cnblogs.com/zhanlang96/p/3859439.html 先上代码 #include "stdafx.h" #i ...

  7. c++学习笔记(2)类的声名与实现的分离及内联函数

    一.类的声名与实现的分离: 和c函数声明与实现分离类似 有.h : 类的声明 .cpp : 类的实现 在在一个类的cpp中应该包含本类的.h文件 在cpp中类的使用:例: //Circle类 //Ci ...

  8. php正则常用表达式

    []里的.相当于\. 涉及到换行一般考虑用模式修正符s s (PCRE_DOTALL) 如果设置了这个修饰符, 模式中的点号元字符匹配所有字符, 包含换行符. 如果没有这个 修饰符, 点号不匹配换行符 ...

  9. 使用python实现最优化问题

    最优化问题 1.无约束的最优化问题 所谓的无约束优化问题指的是一个优化问题的寻优可行集合是目标函数自变量的定义域,即没有外部的限制条件.例如,求解优化问题 [ \begin{array}{rl} \t ...

  10. java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

    /** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...