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();
        }
    }
}

效果:

相关推荐
IT_陈寒17 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
Honor丶Onlyou17 小时前
VS Code 右键菜单修复记录
前端
PILIPALAPENG17 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
JYeontu17 小时前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css
张就是我10659217 小时前
从前端角度理解 CVE-2026-31431
前端
AGoodrMe17 小时前
swift基础之async/await
前端·ios
irving同学4623818 小时前
从零搭建生产级 RAG:Embedding、Chunking、Hybrid Search 与 Reranker
前端·后端
卡卡军18 小时前
vue3-sketch-ruler v3 升级详解:从 Vue 组件到跨框架标尺引擎
前端
还有多久拿退休金18 小时前
让看不见的 AI 动手画画——我意外造出了一个"绘图 Agent"
前端
陆枫Larry18 小时前
一次 iOS 橡皮筋弹性滚动的排查:从 absolute 到 fixed
前端