简体   繁体   English

从PHP创建PDF文件

[英]Create PDF file from PHP

I am trying to create an PDF file from a PHP page with jsPDF but it is not working and I dont know whats wrong. 我正在尝试使用jsPDF从PHP页面创建一个PDF文件,但它不起作用,我不知道什么是错的。

Can someone help me? 有人能帮我吗?

First I have a Iframe . 首先我有一个Iframe The page I want to convert is displayed in the Iframe: 我要转换的页面显示在iframe中:

Iframe: IFRAME:

<?php
    <iframe id="frame" name="frame" width="100%" height="1160px" frameborder="0" src="./page.php?id=' . $_GET['id'] . '"></iframe>
    <button id="cmd">Button</button>
?>

When I hit Button the following script should convert the page to PDF 当我按下Button ,以下脚本应将页面转换为PDF

Script 脚本

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<script type="text/javascript">
    var doc = new jsPDF();

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

    $('#cmd').click(function () {
        doc.fromHTML($('frame').get(0), 15, 15, {
            'width': 170, 
            'elementHandlers': specialElementHandlers
        });

    doc.save('sample-file.pdf');
    });
</script>

But when I hit Button . 但是当我点击Button Nothing is happening. 什么都没发生。 Does someone know whats wrong? 有人知道什么是错的吗?

You must be get html of iframe, so use contents() function and then get documentElement of iframe: 你必须获得iframe的html,所以使用contents()函数然后获取iframe的documentElement

<iframe id="frame" name="frame" width="100%" height="1160px" frameborder="0" src="http://localhost/"></iframe>
<button id="cmd">Button</button>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<script type="text/javascript">
    var doc = new jsPDF();

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

    $('#cmd').click(function () {
        doc.fromHTML($('#frame').contents().find('html').html(), 15, 15, {
            'width': 170, 
            'elementHandlers': specialElementHandlers
        });

    doc.save('sample-file.pdf');
    });
</script>

Note: If your iframe source page or part of it protected with x-frame-options header, you cannot access to html of it. 注意:如果您的iframe源页面或其中的一部分受x-frame-options标头保护,则无法访问它的html。

<button onclick="cmd()">Button</button>

Change this to 将此更改为

<button id="cmd">Button</button>

You are calling the click event in JS on the id. 您正在使用id调用JS中的click事件。 Or change the onclick event to a function with the name cmd. 或者将onclick事件更改为名为cmd的函数。

Apparently, jsPDF has limitations and/or bugs. 显然,jsPDF有局限性和/或错误。 It has problems with complex tables (especially with colspans). 复杂的表(特别是使用colspans)存在问题。 It throws javascript errors which stops the renderer which is why you're getting blank pdfs. 它抛出javascript错误,停止渲染器,这就是你得到空白pdf的原因。

https://github.com/MrRio/jsPDF/issues/516 https://github.com/MrRio/jsPDF/issues/516

https://github.com/MrRio/jsPDF/issues/595 https://github.com/MrRio/jsPDF/issues/595

A fork has been posted here: https://github.com/jutunen/jsPDF-table_2 but I'm not sure if it addresses other limitations of jsPDF. 这里发布了一个fork: https//github.com/jutunen/jsPDF-table_2但是我不确定它是否解决了jsPDF的其他限制。

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

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