netcore html to pdf

一、新建项目:QuestPDFDemo

 

二、上代码


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 _logger;

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

        public IActionResult Index()
        {
            var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.pdf");
            var htmlContent = "你好呀!";
            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();
        }
    }
}

效果:

netcore html to pdf_第1张图片

你可能感兴趣的:(ASP.NET,Core,html,pdf,前端)