zoukankan      html  css  js  c++  java
  • 文章内容过长,将此内容转为pdf的方式(使用node)

    第一步:先下载 html-pdf,命令为 npm i html-pdf -D

    var pdf = require('html-pdf');

    第二部:创建一个pdf模板

    var html1 = fs.readFileSync('./routes/1.html','utf8')  //模板1

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <style>
        em {
            font-style: normal;
        }
        .details-box {
            padding: 15px;
        }
    
        .details-telit h2 {
            font-size: 20px;
            text-align: center;
        }
    
        .details-source {
            text-align: center;
            font-size: 14px;
            margin-top: 30px;
        }
    
        .details-source span {
            padding: 0 15px;
            color: #666;
        }
    
        /* 标题 */
        .details-title {
            font-size: 18px;
            padding: 0;
            border-bottom: 1px solid #ccc;
            position: relative;
            padding-left: 20px;
            padding-bottom: 8px;
        }
        /* 内容 */
        .details-matters {
            padding: 30px 0;
            text-align: justify;
            font-size: 12px;
            line-height: 28px;
            color: #666;
        }
        .details-title::after {
            content: '';
             5px;
            background: #121937;
            position: absolute;
            top: 3px;
            bottom: 11px;
            left: 0;
        }
        .tedt-hhs {
            color: #666666;
        }
    </style>
    <body>
      <div class="details-box">
        <div class="details-telit">
          <h2>
            __title__
    
          </h2>
        </div>
        <div class="details-matter">
          <div class='details-title'>
            内容
           </div>
           <div class="details-matters">
            __content__
           </div>
        </div>
      </div>
    </body>
    
    </html>
    var html = fs.readFileSync('./routes/2.html','utf8')  //模板2
    第三步:对模板转为pdf文件的方法进行封装:
    // 生成pdf
    function createPDFProtocolFile(template, options, reg, filename) {
        /**
          template: html 模板
          options: 配置
          reg: 正则匹配规则
          filename: 输出pdf文件路径及文件名
        */
          // 将所有匹配规则在html模板中匹配一遍
          if (reg && Array.isArray(reg)) {
            reg.forEach(item => {
              template = template.replace(item.relus, item.match);
            });
          }
          pdf
          .create(template, options).toFile(filename, function(err, res) {
              if (err) {
                console.log(err);
                  return err
              }
              console.log(res);
          })
    }

    第四步:使用代码生成pdf文件

    var options = {
        "format": 'A4',
        "header": {
        "height": "10mm",
        "contents": ''
    }}; // 一些配置// 匹配规则
    var reg = [
      {
         relus: /__title__/g,
         match: info[0].title
      },
       {
          relus: /__content__/g,
          match: selectConRs[0].content
       }
    ];
    createPDFProtocolFile(html, options, reg, './public/pdf/'+id+'.pdf'); // 传入参数 生成pdf

     第五步:以上步骤操作完成,就可以实现将文章内容写进html页面,最终转为pdf。

     
  • 相关阅读:
    nginx 配置文件配置
    源码编译php5.4 ./configure参数
    腾讯云Linux VPS新硬盘分区与挂载教程(面板重装不丢失数据)
    十行代码带你量化交易入门
    谱聚类(转)
    随机模拟(MCMC)
    LDA 线性判别分析
    Windows10上安装Keras 和 TensorFlow-GPU
    ASCII 码
    KL距离,Kullback-Leibler Divergence
  • 原文地址:https://www.cnblogs.com/lxz123/p/13564859.html
Copyright © 2011-2022 走看看