DataDoc.App/Helper/DocPdfHelper.cs

(完整代码) 1KB

using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Drawing;
using Aspose.Html.Saving;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataDoc.App.Helper
{
    public class DocPdfHelper
    {
        public DocPdfHelper() { }

        /// <summary>
        /// 创建PDF文件
        /// </summary>
        /// <param name="htmlPath">HTML文件路径</param>
        /// <param name="height">生成PDF的高度</param>
        public static void CreatePDF(string htmlPath, int height)
        {
            try
            {
                string docPath = Application.StartupPath + "\\doc\\";
                if (!Directory.Exists(docPath))
                {
                    Directory.CreateDirectory(docPath);
                }
                string pdfPath = docPath + "\\data_design_pdf_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                var document = new HTMLDocument(htmlPath);
                // 设置页面大小、边距、分辨率及背景颜色 
                var options = new PdfSaveOptions()
                {
                    HorizontalResolution = 200,
                    VerticalResolution = 200,
                    JpegQuality = 100,
                    BackgroundColor = System.Drawing.Color.White
                };
                options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(1600, height), new Margin(20, 10, 10, 10));
                // 将HTML转成PDF
                Converter.ConvertHTML(document, options, pdfPath);
            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex, "CreatePDF");
            }
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。