【问题标题】:Create PDF file from iframe从 iframe 创建 PDF 文件
【发布时间】:2017-06-03 01:02:53
【问题描述】:

我想保存一个包含 IFrame 内容的 PDF 文件。我正在使用 jsPDF。当我点击调用该函数的按钮时,脚本会创建一个空的 PDF 页面。

框架内容如下:https://jsfiddle.net/ss6780qn/

我正在使用以下脚本:

<script src="../jspdf/plugins/standard_fonts_metrics.js"></script>
<script type="text/javascript">
function toPDF(){       
        var pdf = new jsPDF('p', 'in', 'letter');

    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    source = $('#frame')[0]

    // we support special element handlers. Register them with jQuery-style 
    // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
    // There is no support for any other type of selectors 
    // (class, of compound) at this time.
    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        'div': function(element, renderer){
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    }

    // all coords and widths are in jsPDF instance's declared units
    // 'inches' in this case
    pdf.fromHTML(
        source // HTML string or DOM elem ref.
        , 0.5 // x coord
        , 0.5 // y coord
        , {
            'width':7.5 // max width of content on PDF
             ,'elementHandlers': specialElementHandlers
        }
    )

    pdf.save('Test.pdf');
}

有人知道出了什么问题以及我该如何解决这个问题吗?

【问题讨论】:

  • 还没有修复
  • 还没有修复
  • 你能用你的整个代码创建一个 JSFiddle 或等效的,而不仅仅是框架吗?
  • 'specialElementHandlers' 完成了什么。从表面上看,它说忽略所有“div”元素。可能不利于捕获您的内容?内容中的第一个元素是 div class="content"

标签: javascript pdf jspdf


【解决方案1】:

看起来这个问题在 Github 上的一个未决案例,自 2015 年以来仍然开放。请参阅 https://github.com/MrRio/jsPDF/issues/496。也许没有解决办法。

另外,在源代码中,您正在调用的 jsPDFAPI.fromHTML 函数之前有以下符号。请参阅可能影响您工作的最后一点重新表。

* Converts HTML-formatted text into formatted PDF text.
*
* Notes:
* 2012-07-18
* Plugin relies on having browser, DOM around. The HTML is pushed into dom and traversed.
* Plugin relies on jQuery for CSS extraction.
* Targeting HTML output from Markdown templating, which is a very simple
* markup - div, span, em, strong, p. No br-based paragraph separation supported explicitly (but still may work.)
* Images, tables are NOT supported.

另外 - 我试图在这里和 jsfiddle 中设置一个工作的 sn-p,但无法让 jsPDF.fromHTML 工作。我不是 jsPDF 专家,但我一直在这个街区,我不觉得 jsPDF 非常健壮 - 你可能想扩大你的网络并找到另一个插件。只是我的看法。

【讨论】:

    【解决方案2】:

    要从 iframe 打印 html,您需要从 iframe 获取内容。从 iframe 获取内容的源代码(我从this 得到):

    function getFrameContents() {
        var iFrame = document.getElementById('frame');
        var iFrameBody;
        if (iFrame.contentDocument) { // FF
            iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
        } else if (iFrame.contentWindow) { // IE
            iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
        }
        //alert(iFrameBody.innerHTML);
        return iFrameBody.innerHTML
    }
    

    所以你替换:

    source = $('#frame')[0]
    

    source = getFrameContents();
    

    或者简单的jQuery:

    source = $("#frame").contents().find('body')[0];
    

    但是,CORS(跨域资源共享)仍然存在问题。为了解决这个问题,您可以创建 Web 服务器并将您的 html 代码和 iframe src 放在那里,看看它是如何工作的。

    【讨论】:

    • source = getFrameContents(); 替换source = $('#frame')[0] 无效。替换后停止创建 PDF 文件。当我使用source = $("#frame").contents().find('body')[0]; 时,它会创建 PDF 文件,但它仍然是空的
    • 您是否将文件移动到 Web 服务器?
    • 您可以尝试将var pdf = new jsPDF('p', 'in', 'letter'); 替换为var pdf = new jsPDF();
    • 还是一样
    • 这很奇怪,设置在我的电脑上运行。我使用 jsPDF-1.3.2,将 index.html、iframe.html 移动到同一个 Web 服务器中。将var pdf = new jsPDF('p', 'in', 'letter'); 替换为var pdf = new jsPDF(); 并将source = $('#frame')[0] 替换为$("#frame").contents().find('body')[0];
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 2017-11-03
    • 2013-03-26
    • 2011-10-08
    • 2013-09-22
    • 2011-02-03
    • 2015-01-29
    • 2017-05-22
    相关资源
    最近更新 更多