我正在尝试将html页面转换为pdf,但它不是渲染html页面,因为它作为pdf获取目标的页面内容 [英] i am trying to convert html page into pdf but its not rendering html page as it as the content of page in pdf getting desturb

查看:78
本文介绍了我正在尝试将html页面转换为pdf,但它不是渲染html页面,因为它作为pdf获取目标的页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//这是转换代码

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.IO;

使用System.Data;

使用iTextSharp.text;

使用iTextSharp.text.html.simpleparser ;

使用iTextSharp.text.pdf;



命名空间CRM_And_Quotation_System

{

public partial class QuotationPage:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{< br $>


}



protected void Button1_Click(对象发送者,EventArgs e)

{

Response.ContentType =application / pdf;

Response.AddHeader(content-disposition,attachment; filename = ConvertedPDF.pdf);

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

this.Page.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

文件pdfDoc = new Document(PageSize.A2, 4f,4f,4f,0f);

//文档pdfDoc =新文档(PageSize.A3,100f,10f,10f,10f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc,Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

}

}

}

//this is the code of conversion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

namespace CRM_And_Quotation_System
{
public partial class QuotationPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ConvertedPDF.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 4f, 4f, 4f, 0f);
// Document pdfDoc = new Document(PageSize.A3, 100f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
}

推荐答案

试试这些链接 -

ASP.NET中的HTML到PDF [ ^ ]

使用ASP.NET从HTML页面生成PDF文档 [ ^ ]
Try these links -
HTML to PDF in ASP.NET[^]
Generate PDF documents from a HTML page using ASP.NET[^]


这篇关于我正在尝试将html页面转换为pdf,但它不是渲染html页面,因为它作为pdf获取目标的页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆