简体   繁体   English

html2canvas - 是否可以将隐藏的 DIV 打印为 pdf?

[英]html2canvas - is there to print a hidden DIV to pdf?

Background背景

I have a table of records that uses the "tabulator" control to allow users to sort, filter etc. once they have the results they want in the table, I need to provide a way for users trigger the creation of a PDF per row, using a PDF template.我有一个记录表,它使用“制表符”控件来允许用户排序​​、过滤等。一旦他们在表中获得了他们想要的结果,我需要为用户提供一种方法来触发每行创建一个 PDF,使用 PDF 模板。 At the top of the page, I have a drop down menu where they can select a template.在页面顶部,我有一个下拉菜单,他们可以在其中选择模板。 For example, it will have options like this:例如,它将有如下选项:

  • pdf with blue logo带有蓝色标志的pdf
  • pdf with pink logo带有粉红色标志的pdf

etc.等等。

What I know how to do: 1. I know how to loop through the selected records in my table in jquery 2. I know how to create a basic jsPDF document.我知道怎么做: 1. 我知道如何在 jquery 2. 我知道如何创建一个基本的 jsPDF 文档。 3. The "template" mechanism will simply be 2 different that are predefined in the webpage. 3.“模板”机制将只是网页中预定义的两种不同。 Depending on which option they select in the dropdown, I can determine which logo to include.根据他们在下拉列表中选择的选项,我可以确定要包含哪个徽标。

Problem问题

It seems that unless the DIVs that will be included in the PDF are visible, html2canvas won't work.似乎除非将包含在 PDF 中的 DIV 可见,否则 html2canvas 将无法工作。

So far, this is the prototype code I've been playing with: (other than the fact that the DIV i'm trying to PDF shows on the screen, everything else works.)到目前为止,这是我一直在玩的原型代码:(除了我试图 PDF 的 DIV 显示在屏幕上的事实之外,其他一切都有效。)

<div id="bluetemplate" class="ug_logo_style" style="display: none">
    <img class="bluelogo"></img>
</div>
<div id="pinktemplate" class="ug_logo_style" style="display: none">
    <img class="pinklogo"></img>
</div>

$("#templatedropdown").change(function(){
   var selectedtemplate = this.value;
   switch (selectedtemplate){
        case 'blue':
            $("div#bluetemplate").show();
            $("div#pinktemplate").hide();
            break;
        case 'pink':
            $("div#pinktemplate").show();
            $("div#bluetemplate").hide();
            break;
   }
});

$("#btntemplate").click(function(){
    switch($('#template option:selected').val()){
        case 'blue':            
            var imgData;
            html2canvas($("#bluetemplate"), {
                useCORS: true,
                onrendered: function (canvas) {
                    imgData = canvas.toDataURL(
                       'image/png');
                    var doc = new jsPDF({
                        orientation: 'landscape', 
                        unit:'pt', 
                        format:[400,200]});
                    doc.addImage(imgData, 'PNG', 10, 10);

                    doc.text(registrant.first_name + " " + registrant.last_name, 10, 100);
                    doc.text(registrant.email, 10, 120);                   
                    doc.save(registrant.event_id + '_' + registrant.id + '.pdf');
                    window.open(imgData);
                }
            });

            break;
        case 'pink':
            var imgData;
            html2canvas($("#pinktemplate"), {
                useCORS: true,
                onrendered: function (canvas) {
                    imgData = canvas.toDataURL(
                       'image/png');
                    var doc = new jsPDF({
                        orientation: 'landscape', 
                        unit:'pt', 
                        format:[400,200]});
                    doc.addImage(imgData, 'PNG', 10, 10);

                    doc.text(registrant.first_name + " " + registrant.last_name, 10, 100);
                    doc.text(registrant.email, 10, 120);                   
                    doc.save(registrant.event_id + '_' + registrant.id + '.pdf');
                    window.open(imgData);
                }
            });

            break;
    }

What you can do is to temporarily display the hidden div, convert to pdf, then hide it again. 您可以做的是暂时显示隐藏的div,将其转换为pdf,然后再次将其隐藏。 It is only a workaround though if it's acceptable to only show the logo for a split second. 尽管仅在瞬间显示徽标是可以接受的,但这只是一种解决方法。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" 
        integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/"
        crossorigin="anonymous"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<!-- html2canvas 1.0.0-alpha.11 or higher version is needed -->
<script>
    function download() {
        let pdf = new jsPDF('p', 'pt', 'a4');
        pdf.setProperties({
            title: 'jsPDF sample'
        }); // used for window.open() only
        let bluetemplate = document.getElementById('bluetemplate');
        bluetemplate.style.display = '';
        pdf.html(document.body, {
            callback: function () {
                //pdf.save('test.pdf');
                window.open(pdf.output('bloburl')); // to debug
                bluetemplate.style.display = 'none';
            }
        });
    }
</script>

Have you tried adding this to your css 您是否尝试过将此添加到CSS

.html2canvas-container { width: 3000px !important; height: 3000px !important; }

Please refer here for issues relating to printing invisible content. 请参阅此处以了解与打印不可见内容有关的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM