前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET 实现Html 生成图片或PDF

.NET 实现Html 生成图片或PDF

作者头像
郑子铭
发布2023-09-19 13:49:24
2980
发布2023-09-19 13:49:24
举报
文章被收录于专栏:DotNet NB && CloudNative

前言

本文将使用PuppeteerSharp组件、实现Html代码片段生成Jpg照片

PuppeteerSharp地址

https://github.com/hardkoded/puppeteer-sharp

1、安装引用PuppeteerSharp

使用Nuget界面管理器

搜索 "PuppeteerSharp",找到对应的包点安装即可。

2、实现一段Html页面

代码语言:javascript
复制
<body>
  <div class="box">
    <h1>考场号:&nbsp;&nbsp;&nbsp;01</h1>
    <h1>座位号:&nbsp;&nbsp;&nbsp;01</h1>
    <div class="center-box">
      <h4>姓&nbsp; &nbsp;&nbsp;名:&nbsp; &nbsp;花卷</h4>
      <h4>身份证号:&nbsp; &nbsp;545645456456465</h4>
      <h4>准考证号:&nbsp; &nbsp;54564545454</h4>
    </div>
    <div class="botom-box">
      <h2>温馨提示:</h2>
      <h3>请核对信息无误后,张贴在准考证指定位置!</h3>
    </div>
  </div>
</body>
<style>
  .box{
    box-sizing: border-box;
    padding-top: 10px;  
    text-align: center;
    font-family: fangsong;
  }
  .box>h1 {
    font-size: 46px;
  }
  .center-box {
    text-align: left;
    width:290px;
    margin:60px auto;
}
   .center-box > h4{
    font-size: 18px;
   }
  .botom-box {
    text-align: center;
    margin-left: 20px;
}
</style>

Html预览效果

C#实现代码

代码语言:javascript
复制
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
});
string destFilePath = AppDomain.CurrentDomain.BaseDirectory + "TestImgCreate\\";
if (!Directory.Exists(destFilePath))
{
    Directory.CreateDirectory(destFilePath);
}
string outputFile = destFilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
using (var page = await browser.NewPageAsync())
{
    await page.SetViewportAsync(new ViewPortOptions
    {
        Width = 425,//图片宽度
        Height = 635//图片高度
    });
    await page.SetContentAsync(@"
    <html>
    <body>
    <div class=\""box\"">
        <h1>考场号:&nbsp;&nbsp;&nbsp;01</h1>
        <h1>座位号:&nbsp;&nbsp;&nbsp;01</h1>
        <div class=\""center-box\"">
        <h4>姓&nbsp; &nbsp;&nbsp;名:&nbsp; &nbsp;花卷</h4>
        <h4>身份证号:&nbsp; &nbsp;545645456456465</h4>
        <h4>准考证号:&nbsp; &nbsp;54564545454</h4>
        </div>
        <div class=\""botom-box\"">
        <h2>温馨提示:</h2>
        <h3>请核对信息无误后,张贴在准考证指定位置!</h3>
        </div>
    </div>
    </body>
    </html>
    <style>
    .box{
        box-sizing: border-box;
        padding-top: 10px;  
        text-align: center;
        font-family: fangsong;
    }
    .box>h1 {
        font-size: 46px;
    }
    .center-box {
        text-align: left;
        width:290px;
        margin:60px auto;
    }
    .center-box > h4{
        font-size: 18px;
    }
    .botom-box {
        text-align: center;
        margin-left: 20px;
    }
    </style>");
    ScreenshotOptions screenshotOptions = new ScreenshotOptions();
    screenshotOptions.FullPage = true; //是否截取整个页面
    screenshotOptions.OmitBackground = false;//是否使用透明背景,而不是默认白色背景
    screenshotOptions.Quality = 100; //截图质量 0-100(png不可用)
    screenshotOptions.Type = ScreenshotType.Jpeg; //截图格式
    await page.ScreenshotAsync(outputFile, screenshotOptions);
}

生成之后发现样式没有进行渲染,效果如下

发现html中的样式丢失了,后将Html中的双引号替换成单引号后,样式可以正常展示,效果如下

如果需要生成PDF文件,只需要进行以下修改

代码语言:javascript
复制
await page.ScreenshotAsync(outputFile, screenshotOptions);
->
await page.PdfAsync(outputFile);
文件名称修改成PDF后缀

推荐阅读:

摸鱼神器,使用 C# 和 WPF 开发的统计小工具 Tai

.NET开源最全的第三方登录整合库 - CollectiveOAuth

【对.NET系统架构改造的一点经验和教训】的技术要点的看法

对.NET系统架构改造的一点经验和教训

一个.NET 7 + DDD + CQRS +React+Vite的实战项目

程序员开发者神器:10个.Net开源项目

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-09-17 08:00,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DotNet NB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档