Wkhtmltopdf分页符不能正常工作 [英] Wkhtmltopdf page break is not working proper
问题描述
Hello All,
使用带有分页功能的HTML字符串生成PDF不正常工作...下面是我的代码和输出
Default.aspx
<%@ Page Language =C#AutoEventWireup =trueCodeBehind =Default.aspx。 csInherits =wkhtmltopdfTest._Default%>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< title>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< asp:Button ID =Button1runat =serverText =DownloadOnClick = btnExport_Click/>
Hello All ,
Generate PDF using HTML string with page break is not proper working ... Below is my Code and Output
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="wkhtmltopdfTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<asp:Button ID="Button1" runat="server" Text="Download" OnClick="btnExport_Click" />
< asp:Panel ID =pnlPersonrunat =server>
<asp:Panel ID="pnlPerson" runat="server">
b / b
First Page |
第二页 |
第三页 |
第四页 |
五页 |
First Page |
Second Page |
Third Page |
Fourth Page |
Five Page |
< br $> b $ b
Default.cs.aspx
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web .UI;
使用System.Web.UI.WebControls;
使用System.Diagnostics;
使用System.IO;
使用System.Text;
使用System.Text.RegularExpressions;
namespace wkhtmltopdfTest
{
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
protected void btnExport_Click(object sender,EventArgs e)
{
尝试
{
string PrintStyle = System.IO.File.ReadAllText(Server.MapPath(/ PrintStyle.css));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
string outXml =+
< style type = \text / css \> + PrintStyle ++
Default.cs.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace wkhtmltopdfTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
string PrintStyle = System.IO.File.ReadAllText(Server.MapPath("/PrintStyle.css"));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
string outXml = " " +
"<style type=\"text/css\"> " + PrintStyle + " " +
"
"
;
// table,tr,td,th,tbody,thead,tfoot {page-break-inside:avoid!important;}
// tr {display:block;}
byte [] result = ConvertHtmlToPdf(outXml);
HttpContext.Current .Response.Clear();
Response.ContentType =application / pdf;
string outputFilename =Report+_+ DateTime.Now.ToString( yyyy-MM-dd-hh-mm-ss-fff)+。pdf;
Response.AddHeader(content-disposition,attachment; filename =+ outputFilename);
Response.CacheControl =No-cache;
MemoryStream ms = new MemoryStream(result);
ms.WriteTo(Response.OutputStream );
Response.Flush();
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch(例外情况)
{
}
}
protected byte [] ConvertHtmlToPdf(string html)
{
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
string conPath = System.IO.Path.GetDirectoryName(ass.CodeBase);
Uri uri = new Uri(conPath);
conPath = uri.AbsolutePath.Replace(%20,)。Replace(25,);
conPath = Path.GetFullPath(Path.Combine(conPath,@.. \));
conPath = conPath +bin \\wkhtmltopdf.exe;
var p = new流程
{
StartInfo =
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
FileName = conPath
}
};
string s = - q;
s + = - margin-bottom 0;
s + = - margin-right 0;
s + = - margin-left 0;
s + = - margin-top 0 ;
s + = - page-size A4;
s + = - print-media-type;
s + = --disable-smart-shrinking;
s + = - - ;
p.StartInfo.Arguments = s;
p .Start();
StreamWriter myStreamWriter = new StreamWriter(p.StandardInput.BaseStream,Encoding.UTF8);
myStreamWriter.Write(html);
myStreamWriter.Close();
var buffer = new byte [32768];
byte [] file;
using(var ms = new MemoryStream())
{
while(true)
{
var read = p。 StandardOutput.BaseStream.Read(buffer,0,buffer.Length);
if(read< = 0)
{
break;
}
ms.Write(缓冲区,0,读取);
}
file = ms.ToArray ();
}
p.WaitForExit(60000);
var returnCode = p.ExitCode;
p.Close();
返回returnCode == 0? file:null;
}
}
}
PrintStyle.css
@media print
{
tr.page-break { display:block; page-break-after:always; }
}
我的尝试:
我已经运行了上面的代码但是我能够通过分页来获得正确的报告。
";
//table, tr, td, th, tbody, thead, tfoot { page-break-inside: avoid !important;}
//tr{display:block;}
byte[] result = ConvertHtmlToPdf(outXml);
HttpContext.Current.Response.Clear();
Response.ContentType = "application/pdf";
string outputFilename = "Report" + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + outputFilename);
Response.CacheControl = "No-cache";
MemoryStream ms = new MemoryStream(result);
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch(Exception ex)
{
}
}
protected byte[] ConvertHtmlToPdf(string html)
{
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
string conPath = System.IO.Path.GetDirectoryName(ass.CodeBase);
Uri uri = new Uri(conPath);
conPath = uri.AbsolutePath.Replace("%20", " ").Replace("25", "");
conPath = Path.GetFullPath(Path.Combine(conPath, @"..\"));
conPath = conPath + "bin\\wkhtmltopdf.exe";
var p = new Process
{
StartInfo =
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
FileName = conPath
}
};
string s = "-q ";
s += "--margin-bottom 0 ";
s += "--margin-right 0 ";
s += "--margin-left 0 ";
s += "--margin-top 0 ";
s += "--page-size A4 ";
s += "--print-media-type ";
s += "--disable-smart-shrinking ";
s += "- -";
p.StartInfo.Arguments = s;
p.Start();
StreamWriter myStreamWriter = new StreamWriter(p.StandardInput.BaseStream, Encoding.UTF8);
myStreamWriter.Write(html);
myStreamWriter.Close();
var buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
var read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
p.WaitForExit(60000);
var returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
}
}
PrintStyle.css
@media print
{
tr.page-break {display: block;page-break-after: always; }
}
What I have tried:
I have run the Above code but i am able to get proper report with page break .
推荐答案
明确的分页符(使用CSS'分页符 - wqtmltopdf不支持'规则'之后的表格(在你的情况下为tr标签)。
作为替代方案,您可以将表格拆分为多个表格并强制分页使用
Explicit page break (with CSS 'page-break-after' rule) inside tables (tr tag in your case) is not supported by wkhtmltopdf.
As alternative you can split your table to several tables and force page break between them with
<div style='page-break-after:always;'></div>
这篇关于Wkhtmltopdf分页符不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!