html导出word input,html页面直接导出word pdf

直接上代码

html

///这里放想要导出的html 元素

function ExportWord() {

var html = $("#div_workInfo").html();

$("#hidWord").val(encodeURIComponent(html));

$("#formWord").submit();

} ///from 表单方式提交这段html

接下来就是后台接收了

[HttpPost]

public ActionResult ExportWord(FormCollection form)

{

string html = Server.UrlDecode(form["hidWord"]);///接收前台的html的

byte[] pdfFile = this.ConvertHtmlTextToPDF(html);///pdf 执行这个就可以了

///这里执行word 操作

sb.Append("");

sb.Append("

");

sb.Append("");

sb.Append("

");

sb.Append("

");

sb.Append(html);

sb.Append("

");

sb.Append(""); 说明一下 加了style 可以设置页面方式为横向 加html 那边 可以设置web视图为 页面视图

return File(pdfFile, "application/pdf", "" + "报销表_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");

}

这里用到了 iTextSharp 直接可以直接在项目nuget 里面下载安装

///

/// 将Html文字 输出到PDF档里

///

///

///

public byte[] ConvertHtmlTextToPDF(string htmlText)

{

if (string.IsNullOrEmpty(htmlText))

{

return null;

}

//避免当htmlText无任何html tag标签的纯文字时,转PDF时会挂掉,所以一律加上

标签

htmlText = "

" + htmlText + "

";

MemoryStream outputStream = new MemoryStream();//要把PDF写到哪个串流

byte[] data = Encoding.UTF8.GetBytes(htmlText);//字串转成byte[]

MemoryStream msInput = new MemoryStream(data);

Document doc =new Document();//要写PDF的文件,建构子没填的话预设直式A4

PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);

//指定文件预设开档时的缩放为100%

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);

//开启Document文件

doc.Open();

//使用XMLWorkerHelper把Html parse到PDF档里

XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new Models.UnicodeFontFactory());//UnicodeFontFactory 这个类是防止中午问题的 我在下面贴一下 直接复制过去就OK了

//将pdfDest设定的资料写到PDF档

PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

writer.SetOpenAction(action);

doc.Close();

msInput.Close();

outputStream.Close();

//回传PDF档案

return outputStream.ToArray();

}

UnicodeFontFactory 类 的内容 继承FontFactoryImp 类

public class UnicodeFontFactory : FontFactoryImp

{

private static readonly string arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),

"arialuni.ttf");//arial unicode MS是完整的unicode字型。

private static readonly string 标楷体Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),

"KAIU.TTF");//标楷体

public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color,

bool cached)

{

//可用Arial或标楷体,自己选一个

BaseFont baseFont = BaseFont.CreateFont(标楷体Path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

return new Font(baseFont, size, style, color);

}

}

///导出的pdf 这个是百度上看到的自己抄了一下 发表主要是为了自己收集下来 发表的内容这里都有用过是有效果的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值