netcore html to pdf

2024-01-09 23:31:17

一、新建项目:QuestPDFDemo

 <PackageReference Include="NReco.PdfGenerator" Version="1.2.1" />

二、上代码


using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using QuestPDFDemo.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace QuestPDFDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.pdf");
            var htmlContent = "<html><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><body>你好呀!</body></html>";
            var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
            var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
            System.IO.File.Delete(path);
            System.IO.File.WriteAllBytes(path, pdfBytes.ToArray());
            return View();
        }
    }
}

效果:

文章来源:https://blog.csdn.net/xiaoxionglove/article/details/135430687
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。