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. Warm up

    hdu4612:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:给你一个无向连通图,问加上一条边后得到的图的最少的割边数; 题解:首先对原图求割边数 ...

  2. hOAuth2.0认证和授权原理

    原文地址: http://www.6zou.net/tech/what_is_oauth.html http://www.phpddt.com/%E4%BA%8C%E6%AC%A1%E5%BC%80% ...

  3. MFC 仿QQ聊天软件(黄花寒)

    http://blog.csdn.net/lh844386434/article/details/6655080 http://download.csdn.net/download/lh8443864 ...

  4. Scanner 与 Readable 的read()方法

    Readable接口中的read()方法实现了将字符串读入charBuffer中,但是只有在需要输出的时候才会调用. Scanner是文本扫描器类,利用Scanner扫描并输出charBuffer中的 ...

  5. 【HDOJ】3397 Sequence operation

    线段树的应用,很不错的一道题目.结点属性包括:(1)n1:1的个数:(2)c1:连续1的最大个数:(3)c0:连续0的最大个数:(4)lc1/lc0:从区间左边开始,连续1/0的最大个数:(5)rc1 ...

  6. 【HDOJ】前三百留念

    4个月不到的时间,终于刷到了HDOJ前三百.肯定还不够,好多基本的算法还不了解.还得继续学习.以此留念,假期目标是前一百.

  7. activity+fragment多次切换出现页面空白问题

    刚上手一个项目 懒的用viewpager+fragment模式,尽管在后面的项目中还是用到viewpager+fragment.先说说问题,多次切换fragment的时候页面出现空白,刚开始以为传递的 ...

  8. jqGrid简单介绍

    一.要引用的文件 要使用jqGrid,首先页面上要引入如下css与js文件. 1.css <link href="/css/ui.jqgrid.css" rel=" ...

  9. iOS 多线程学习笔记 —— NSOperation

    本文复制.参考自文章:iOS多线程编程之NSOperation和NSOperationQueue的使用 ,主要为了加强个人对知识的理解和记忆,不做他用.原作者声明: 著作权声明:本文由http://b ...

  10. 实现自己的脚本语言ngscript之三:语法设计

    这是第四篇了,之所以隔了这么久才写,一方面是因为最近开始实习了,另一方面是因为设计语法真是要考虑很多东西. 于是我去读了这本书,里面实现了两种语言,一种跟js差不多语法,用ast解释执行:另一种语法类 ...