.NET 实现Html 生成图片或PDF

前言

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

PuppeteerSharp地址

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

1、安装引用PuppeteerSharp

使用Nuget界面管理器

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

2、实现一段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>
<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预览效果

040affbfe49963324a1d17e0aee0043b.png

C#实现代码

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

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

a964b6904e5db8a95b11cdc1a6259dd4.png

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

9eedbaef450ebf492a9713b96dd45f55.png

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

await page.ScreenshotAsync(outputFile, screenshotOptions);
->
await page.PdfAsync(outputFile);
文件名称修改成PDF后缀

转自:小吴的成长之路

链接:cnblogs.com/wuyongfu/p/17243490.html

- EOF -

技术群:添加小编微信dotnet999

公众号:Dotnet讲堂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值