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 分钟前
大文件断点续传
前端
iOS大前端海猫2 分钟前
深入解析 Vue.js 中的选择器:从 id 到类,再到标签选择器
前端·vue.js
SurgeJS7 分钟前
我造了一个轮子:Norm Axios(约定式请求)
前端·vue.js
USER_A00124 分钟前
【VUE3】练习项目——大事件后台管理
前端·vue.js·axios·pinia·elementplus·husky·vuerouter4
fruge27 分钟前
Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.
前端·css·sass
鸿蒙场景化示例代码技术工程师43 分钟前
基于AssetStoreKit实现免密登录鸿蒙示例代码
前端
在掘金44 分钟前
【kk-utils】Excel工具——excel-js
前端·excel
Danny_FD1 小时前
Canvas的应用与实践
前端·javascript
_请输入用户名1 小时前
husky 切换 simlple-git-hook 失效解决方法
前端
前端九哥1 小时前
🚀Vue 3 hooks 每次使用都是新建一个实例?一文彻底搞懂!🎉
前端·vue.js