【问题标题】:print a multiple divs and its data and save it in pdf打印多个 div 及其数据并将其保存为 pdf
【发布时间】:2015-12-18 07:27:21
【问题描述】:

我想复制/保存/打印多个 div 及其内容并将其保存在 pdf 文件中,使用 jspdf 时遇到对齐和复制图像的问题。

有什么建议可以打印 div 并将其保存为 pdf 文件吗?

<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($(elem).html());
        //alert($(elem).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
      //  mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        //mywindow.close();

        return true;
    }

</script>

以上代码将详细信息保存在 xps 扩展名中,在此之前我想将详细信息捕获到 pdf 文件中?它需要像一般的 pdf 下载一样

请给点建议,如何将可打印的数据保存为pdf?

【问题讨论】:

  • 请向我们展示您的尝试。因此,我们可以根据您的发现提出建议。
  • 嗨StijinvanGaal,是否有可能将元素详细信息打印到pdf文件中?

标签: javascript jquery


【解决方案1】:

去看看jsPDF。这将为您创建 pdf 提供一些支持。

你可以像这样创建一个pdf HTML:

<div id="content">
     <h3>Hello, this is a H3 tag</h3>

    <p>a pararaph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

JavaScript:

var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});

编辑 也看看html2canvas,有教程here。 据我所读,这应该是当前 DOM 的某种屏幕截图。也许这会有所帮助

【讨论】:

  • 我在 jsPdf 上工作,因为它没有从亚马逊捕获图像 url,甚至在页面中我也有谷歌图表显示,这些图表没有使用 jsPdf,除了打印它,我没有任何选择,甚至打印没有吸引 div 滚动内容....任何其他建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 2012-07-10
相关资源
最近更新 更多