简体   繁体   English

另存为 PDF 与 html2Canvas 和 jsPDF 用于 php 应用程序工作 1 次 3

[英]Save as PDF with html2Canvas and jsPDF for php application works 1 time in 3

I would like to save an HTML page as PDF so I used html2canvas and jsPDF to do it.我想将 HTML 页面保存为 PDF 所以我使用 html2canvas 和 jsPDF 来完成它。

The web application is in php and use Bootstrap 4 On 1 side (1 table + 1 graph in chart.js) it works like a dream but on the other side, a view with multiple blocks it works 1 time in 3 and I would know why it's not stable... web 应用程序在 php 中并使用 Bootstrap 4 在一侧(chart.js 中的 1 个表格 + 1 个图形)它像梦一样工作,但在另一侧,一个包含多个块的视图在 3 中工作 1 次,我会知道为什么不稳定...

I search on google and there's no CSS transformation that could make html2canvas bug, I think html2canvas could have some trouble to read the DOM on this side but need another point of view我在谷歌上搜索,没有 CSS 转换可能导致 html2canvas 错误,我认为 html2canvas 可能在读取这边的 DOM 时遇到一些麻烦,但需要另一个观点

Examples: 1st side: 1 side that works示例:第一面: 1 面有效

2nd side: 2nd side that works 2nd side that doesn't work... Here's my code:第 2 面:第 2 面有效第 2 面无效……这是我的代码:

                    <div id="mad" class="row">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 px-1">
                            <div class="row">
                                <?php $this->load->view('block/rapport/mm_ma', $mad_data); ?>
                            </div>
                            <div class="row">
                                <?php $this->load->view('block/rapport/comments_section', array('block_id' => 'mad', 'comments' => $mad_data['comments'])); ?>
                            </div>
                        </div>
                    </div>
                    <div id="pdr" class="row">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 px-1">
                            <div class="row">
                                <?php $this->load->view('block/rapport/mm_ma', $pdr_data); ?>
                            </div>
                            <div class="row">
                                <?php $this->load->view('block/rapport/comments_section', array('block_id' => 'pdr', 'comments' => $pdr_data['comments'])); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

        </div>

My function to export as PDF:我的 function 导出为 PDF:

        $(".loading-spinner-container").removeClass('d-flex').addClass('d-none'); //cache le loading spinner
        html2canvas(document.querySelector("body"), {scrollX: 0, scrollY: 0}).then(canvas => {
            var dataURL = canvas.toDataURL();
            var pdf = new jsPDF("L");
            var width = pdf.internal.pageSize.getWidth();
            var height = pdf.internal.pageSize.getHeight();
            pdf.addImage(canvas, 'JPG', 0, 0, width, height);
            pdf.save("<?= ucfirst($this->uri->segment(2) . '/' . $this->uri->segment(3, '')) . "_" . date("Ymd") . ".pdf" ?>");
            show_content();
        });
    }

I hide the sidebar and the topbar because I noticed that it could make html2canvas to shift the image but now some cells comes out of their box and sometimes don't...我隐藏了侧边栏和顶栏,因为我注意到它可以让 html2canvas 移动图像,但现在有些单元格从它们的盒子里出来,有时不会......

Thanks for the help!谢谢您的帮助!

Solved the problem by setting the option of html2canvas 'foreignObjectRendering' = true通过设置 html2canvas 'foreignObjectRendering' = true 的选项解决了这个问题

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

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