使用nodejs对html2canvas的类似解决方案 [英] A similar solution to html2canvas with nodejs

查看:1180
本文介绍了使用nodejs对html2canvas的类似解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用css,jquery和nodejs(带有ejs和express)创建网站,我需要导出一个包含一些文本的div和一些具有背景图像的div作为图像(jpg或png),但是最好是pdf.我尝试使用html2canvas,但随后我读到它与nodejs不兼容. 我也尝试使用jspdf,但它不会在pdf文件中导出css. 因此,我想知道是否有人知道可以使用nodejs做到这一点的解决方案.

I'm using css, jquery and nodejs (with ejs and express) to create a website and I need to export a div with some text and some divs with a background-image as an image (jpg or png) but the best would be pdf. I tried to use html2canvas but then I read that it is not compatible with nodejs. I also tried with jspdf but it doesn't export the css in the pdf file. So I would like to know if anyone knows a solution that can do that with nodejs.

以下是我的ejs代码的一个示例:

Here is an exemple of my ejs code :

<%for(var j = 0; j < rawData.scanner.length; ++j) {%>
<div class="grid" id="grid-<%= data.scanner[j].camera%>" 
    style="
    width : <%= parseFloat(data.widthScreen) + 17%>px; 
    height : <%= parseFloat(data.heightScreen) + 17%>px; 
    position : absolute;
    left : 0px;
    top : 60px;
    overflow-x : scroll;
    overflow-y : scroll;
    display : none;">
    <%for(var i = 0; i < data.scanner[j].parts; ++i) {%>
    <div class="fenetre" id=<%= data.scanner[j].name + "-img" + i%>
        style="
        background-image : url(<%= (data.scanner[j].imagePath)%>);
        width : <%= data.scanner[j].width%>px; 
        height : <%= data.scanner[j].height%>px; 
        position:absolute; 
        top: 0px; 
        left: <%= (i * data.scanner[j].left)%>px;"
    >
    </div>
    <%}%>
</div>
<%}%>

推荐答案

我知道这个问题很旧,但是最近我会尝试木偶

I know this question is old, but these days I'd try puppeteer

const puppeteer = require("puppeteer");

async function printPDF() {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto("http:///stackoverflow.com", {
    waitUntil: "networkidle0",
  });
  const pdf = await page.pdf({
    width: 1200,
    height: 1920,
    pageRanges: "1-2",
    path: "so.pdf",
  });

  await browser.close();
  return pdf;
}
printPDF();

另请参见使用Node.js的HTML至PDF

这篇关于使用nodejs对html2canvas的类似解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆