首页 > 解决方案 > 如何在浏览器中使用`https://github.com/ovvn/dom-to-pdf`

问题描述

我喜欢https://github.com/ovvn/dom-to-pdf在项目中使用。

到目前为止,我使用https://github.com/tsayen/dom-to-imagehttps://github.com/tsayen/dom-to-imagehttps://github.com/ovvn/dom-to-pdf中是必需的)

像这样

window.print = function () {
    return domtoimage.toPng(document.body)
        .then(function (dataUrl) {
            var link = document.createElement('a');
            link.download = map.printControl.options.documentTitle || "exportedMap" + '.png';
            link.href = dataUrl;
            link.click();
        });
};

https://github.com/ovvn/dom-to-pdf不提供可以集成到我的 HTML 文档中的 JavaScript 文件。这就是为什么我自己用https://github.com/browserify/browserify.

  1. 我安装了browserify全局
  2. 我克隆了回购https://github.com/ovvn/dom-to-pdf
  3. 我跑browserify index.js -o bundle.js

现在我有了 JavaScript 文件,并在我的 HTML 文件中使用它:

<script src="./bundle.js"></script>

它已正确加载。但我不知道如何在我的项目中使用它。

我试过了:

window.print = function () {
    return domtoimage.toPng(document.body)
        .then(function (dataUrl) {
            var link = document.createElement('a');
            link.download = map.printControl.options.documentTitle || "exportedMap" + '.png';
            link.href = dataUrl;
            link.click();
        });
}

var element = document.getElementById('test');
var options = {
    filename: 'test.pdf'
};
domToPdf(element, options, function () {
    console.log('done');
});

在第一种情况下(在 domtopdf 中需要 domtoimage)我得到错误ReferenceError: domtoimage is not defined,在第二种情况下ReferenceError: domToPdf is not defined

如何https://github.com/ovvn/dom-to-pdf在浏览器中使用?

标签: javascriptnode.jspdf

解决方案


您是否使用 npm 安装这些软件包?我可以看看你的进口和/或要求声明......


推荐阅读