Html网页生成Pdf
在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的更多相关文章
- pypdf2:下载Americanlife网页生成pdf合并pdf并添加书签
初步熟悉 安装 pip install pypdf2 合并并添加书签 #!/usr/bin/env python3.5 # -*- coding: utf-8 -*- # @Time : 2019/1 ...
- 实践指南-网页生成PDF
一.背景 开发工作中,需要实现网页生成 PDF 的功能,生成的 PDF 需上传至服务端,将 PDF 地址作为参数请求外部接口,这个转换过程及转换后的 PDF 不需要在前端展示给用户. 二.技术选型 该 ...
- tcpdf 将网页生成pdf
需求:需要将HTML页面生成PDF文档 开发语言:PHP 使用TCPDF第三方类库进行生成,下载地址:http://sourceforge.net/projects/tcpdf/ 核心代码: publ ...
- Freemarker + iTextRender 实现根据模板网页生成PDF
#0 背景 工作需要实现导出PDF的功能,在进行简单调研后,我决定采用Freemarker + iTextRender进行实现. 基本思路如下: Freemarker实现根据动态数据渲染出需要导出的H ...
- 利用SelectPdf插件将网页生成PDF
简介 适用于.NET Framework和.NET Core的HTML至PDF转换器 SelectPdf提供的在线html到pdf转换器使用.NET的Select.Pdf库中的html到pdf转换器. ...
- wkhtmltopdf 将网页生成pdf文件
先安装依赖 yum install fontconfig libXrender libXext xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi freetype l ...
- PHP 生成PDF
一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...
- 采用TuesPechkin生成Pdf
1.需求 前段时间有个需求,要求把网页生成pdf,找了各种插件,才决定使用这个TuesPechkin,这个是后台采用C#代码进行生成 2.做法 我要做的是一个比较简单的页面,采用MVC绑定,数据动态加 ...
- 动态将ASPX生成HTML网页并将网页导出PDF
1.首先要找到wnvhtmlconvert.dll这个文件,并引入项目中. 2.Server.Execute("pos.aspx?id=" + ids); 执行相应的aspx网页 ...
随机推荐
- MacOS快捷键
- Android的系统架构
转自Android的系统架构 从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层,分别介绍如下: 1)应用程序层 ...
- jQuery EasyUI parser 的使用场景
转自原文地址:http://www.easyui.info/archives/216.html parser,故名意思,就是解析器的意思,别看他只有那么几行代码,jQuery Easyui 能够根据c ...
- WMDestroy函数调用inherited,难道是为了调用子类覆盖函数?还有这样调用的?
又碰到了: procedure TWinControl.WMDestroy(var Message: TWMDestroy); begin inherited; // important7 fixme ...
- Git fork指令
ork并且更新一个仓库 现在有这样一种情形:有一个叫做Joe的程序猿写了一个游戏程序,而你可能要去改进它.并且Joe将他的代码放在了GitHub仓库上.下面是你要做的事情: fork并且更新GitH ...
- ORACLE数据缓冲区DB cache
DB CACHE是以数据块为单位组织的缓冲区,数据库刚刚启动的时候,DB CACHE中几乎没有用户数据的缓冲,当会话访问数据库中的表或索引时,首先会检查DB CACHE中是否存在该数据,如果不存在,就 ...
- WordPress Event Easy Calendar插件多个跨站请求伪造漏洞
漏洞名称: WordPress Event Easy Calendar插件多个跨站请求伪造漏洞 CNNVD编号: CNNVD-201309-083 发布时间: 2013-09-11 更新时间: 201 ...
- mysql中的timestamp类型时间比较:unix_timestamp函数
在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...
- 【转】在VMware中安装OS X Yosemite
原文网址:http://blog.gaohaobo.com/229.html OS X(前称:Mac OS X)操作系统是由苹果公司(Apple Inc.)为其Mac系列产品开发的.基于Unix的专属 ...
- Linux下USB烧写uImage kernel
Linux下USB烧写uImage kernel 1.启动开发板,进入u-boot:(如果开发板中没有系统,可以通过用SD卡方式启动开发板进入) U-Boot 2011.06 (Mar 19 ...