.NET 使用 itext7.pdfhtml把html转成pdf

.NET 使用 itext7.pdfhtml把html转成pdf

使用环境 vs2022 .net6

需要使用 itext7.pdfhtml,用NuGet管理

可以直接保存成pdf文件,也可以保存成文件流

  • 前端代码
<body>

    <div>
        <div style="margin-top:10px;">
            <input id="saveFile" type="button" value="html保存为pdf文件" onclick="saveFileFun()" />
        </div>
        <div style="margin-top:10px;">
            <button id="saveStream" onclick="saveStreamFun()">html保存为文件流</button>
        </div>
    </div>

    <script>
        function saveFileFun() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {

                }
            }
            xhr.open("post", "../../Test/SaveFile")
            xhr.send();
        }
        function saveStreamFun() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                debugger
                if (this.readyState == 4 && this.status == 200) {
                    debugger
                    var url = window.URL.createObjectURL(new Blob([this.response], {type:"text/plain,charset=UTF-8"}));
                    var link = document.createElement("a");
                    link.style.display = "none";
                    link.href = url;
                    link.setAttribute("download", "ApiDocument.pdf");
                    document.body.appendChild(link);
                    link.click();
                }
            }
            xhr.open("post", "../../Test/SaveStream")
            xhr.responseType = "blob"
            xhr.send();
        }
    </script>
</body>
  • 后端代码
public FileContentResult SaveStream()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("
" + "测试数据" + "
"
); stringBuilder.Append(@"<table style='border-right:solid 0.5px #000000; border-bottom:solid 0.5px #000000;'> <tbody><tr> <th style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'><b>姓名</b> </th> <th style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'><b>年龄</b> </th></tr> <tr> <td style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'>张三 </td> <td style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'>21 </td></tr> </tbody></table>"); var pageSize = PageSize.A4; var properties = new ConverterProperties(); properties.SetCharset("utf-8"); var provider = new DefaultFontProvider(true, true, true); // 第三个参数为True,以支持系统字体,否则不支持中文 properties.SetFontProvider(provider); var mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT); // 指当前设备类型,如果是预览使用SCREEN mediaDeviceDescription.SetWidth(pageSize.GetWidth()); properties.SetMediaDeviceDescription(mediaDeviceDescription); var stream = new MemoryStream(); var writer = new PdfWriter(stream); writer.SetCloseStream(false); HtmlConverter.ConvertToPdf(stringBuilder.ToString(), writer, properties); writer.Close(); stream.Seek(0, SeekOrigin.Begin); stream.Close(); byte[] byteInfo = stream.ToArray();//pdf字节数组 return new FileContentResult(byteInfo, Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/pdf")); } public void SaveFile(string fullFileName) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("
" + "测试数据" + "
"
); stringBuilder.Append(@"<table style='border-right:solid 0.5px #000000; border-bottom:solid 0.5px #000000;'> <tbody><tr> <th style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'><b>姓名</b> </th> <th style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'><b>年龄</b> </th></tr> <tr> <td style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'>张三 </td> <td style='border-top:solid 0.5px #000000; border-left:solid 0.5px #000000; padding:5px;'>21 </td></tr> </tbody></table>"); // FileInfo fi = new FileInfo(fullFileName); FileInfo fi = new FileInfo("E:\\NewPdfFile.pdf"); PdfWriter writer = new PdfWriter(fi); //用法1 -- 使用默认参数直接转换(A4,) //HtmlConverter.ConvertToPdf(stringBuilder.ToString(), writer); //用法2 -- 自定义页面大小、留白尺寸 PdfDocument pdf = new PdfDocument(writer); PageSize a4 = PageSize.A4; a4.ApplyMargins(20, 20, 20, 20, false); //if true the rectangle will expand, otherwise it will shrink pdf.SetDefaultPageSize(a4); ConverterProperties prop = new ConverterProperties(); prop.SetCharset("utf-8"); var provider1 = new DefaultFontProvider(true, true, true); // 第三个参数为True,以支持系统字体,否则不支持中文 prop.SetFontProvider(provider1); HtmlConverter.ConvertToPdf(stringBuilder.ToString(), pdf, prop); }

.NET 使用 itext7.pdfhtml把html转成pdf_第1张图片
.NET 使用 itext7.pdfhtml把html转成pdf_第2张图片

你可能感兴趣的:(后端,插件,前端,c#,前端,visual,studio)