【问题标题】:Can not convert html to png using phantomjs or wkhtmltoimage无法使用 phantomjs 或 wkhtmltoimage 将 html 转换为 png
【发布时间】:2017-04-21 21:54:54
【问题描述】:

我想把html转成png。

这是我的html

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  main {
    width: 600px;
    margin: 100px auto;
  }

  header {
    width: 100%;
    height: auto;
    background-color: red;
    background-size: cover;
    background-repeat: no-repeat;
  }

  header .content {
    width: 80%;
    margin-left: 10%;
    margin-right: 10%;
    display: flex;
    padding: 40px 0;
  }

  .content .column {
    width: 25%;
    margin: auto;
    text-align: center;
  }

  .content .column.lfp-logo {
    width: 30%;
  }

  .content .column img {
    width: 80%;
    height: auto;
  }
  </style>
</head>
<body>
<main>
  <header>
    <div class="content">
      <div class="column"><img
        src="https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg"></div>
      <div class="column lfp-logo"><img
        src="https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg"></div>
      <div class="column"><img
        src="https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg"></div>
    </div>
  </header>
</main>
</body>
</html>

这是 jsfiddle 示例。这实际上是我需要在我的 png 文件中获取的内容jsfiddle

我尝试使用wkhtmltoimage,得到了这张图片

我也尝试使用phantomjs 来获取这张图片 我使用 phantomjs 和 nodejs 模块 webshot

任何人都可以建议我如何将 html 渲染为 png 图像。 我不需要动态 html。我想要渲染的只是静态的。

更新。添加了生成图片的代码

const pug = require('pug');
const child_process = require('child_process');
const fs = require('fs');
const webshot = require('webshot');

const options = {
    data: {
        images: {
            header: 'https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg',
            team1: 'https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg',
            team2: 'https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg',
            competition: 'https://cdn.shopify.com/s/files/1/1237/5652/products/spiderman_3_compression_shirt_3_small.jpg'
        }
    }
};

const html = pug.renderFile('./template/next.pug', options);

console.log(html); // this is html

// webshot with wkhtmltoimage
child_process.exec(`echo "${html}" | wkhtmltoimage --width 600 --disable-smart-width - export/out.png`, (err) => {
    console.log(err);
});

// webshot with phantomjs
webshot(html, 'hello_world.png', {siteType:'html'}, function(err) {
    console.log(err);
});

【问题讨论】:

  • 哥,您忘记包含用于制作屏幕截图的脚本示例代码。
  • 好的 @Vaviloff 我现在会更新

标签: html image phantomjs wkhtmltoimage


【解决方案1】:

我会说制作类似this 的东西。与html2canvas 合作,您可以轻松快速地完成。

    convert_to_img = function (do_download) {
  html2canvas(document.getElementById("contenedor_chimichangas"), {
    //onrender devuelve el canvas para hacerle los retoques necesarios
    onrendered: function (canvas) {//sombra aqui, sombra allí!! 
      var context = canvas.getContext('2d');//context from originalCanvas
      var data_image = canvas.toDataURL("image/png"); //cogemos la url de la imagen sin tratar
      //añadimos la url en una imagen para poder tratarla y redimensionarla
      var image_edicion = $("<img/>", {
        id: "image",
        src: data_image
      }).appendTo($("#show_img").empty());
        //if we click download:
        if(do_download === true){
          Canvas2Image.saveAsPNG(new_canvas);
        }
      });

    },

  });
}

js 逐行记录。 ;D

【讨论】:

  • 我不能使用 Html2Canvas 或其他类似的库。因为对话必须在服务器端
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多