netcore html to pdf

一、新建项目:QuestPDFDemo

复制代码
 <PackageReference Include="NReco.PdfGenerator" Version="1.2.1" />

二、上代码

cs 复制代码
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();
        }
    }
}

效果:

相关推荐
朕的剑还未配妥2 分钟前
移动端触摸事件与鼠标事件的触发机制详解
前端
墨鱼鱼6 分钟前
【征文计划】Rokid JSAR 实践指南:打造沉浸式 "声动空间盒" 交互体验
前端
携欢9 分钟前
Portswigger靶场之Exploiting a mass assignment vulnerability通关秘籍
前端·安全
什么芋泥香蕉33017 分钟前
比 Manus 还好用?这款国产 AI,让 Python 小白也能玩转编程
前端·后端
为java加瓦18 分钟前
前端学AI:如何写好提示词(prompt)
前端·人工智能·prompt
qq_18417767726 分钟前
前端自动部署项目到服务器
服务器·前端·javascript
非凡ghost27 分钟前
猫眼浏览器(Chrome内核增强版浏览器)官方便携版
前端·网络·chrome·windows·软件需求
Never_Satisfied33 分钟前
在JavaScript / HTML / Node.js中,post方式的Content-Type属性的text的三种编码
javascript·node.js·html
C2X41 分钟前
Vue3.0 学习总结
前端
汤姆Tom44 分钟前
CSS 新特性与未来趋势
前端·css·面试