【问题标题】:Generate Downloadable PDF's from HTML Tables [closed]从 HTML 表生成可下载的 PDF [关闭]
【发布时间】:2013-01-26 18:43:09
【问题描述】:

你能推荐一些工具来从 HTML 表格生成可下载的 PDF 吗?

如果可能,与 Twitter Bootstrap 兼容。

Ps.:我试过 Prawn,但我想知道一些其他工具。

【问题讨论】:

    标签: html twitter-bootstrap pdf html-table


    【解决方案1】:

    您需要了解的内容

    基本 Perl 脚本、HTML、Perl 模块安装

    简介

    Adobe © PDF 文件是传输文档的流行文件格式之一。其原因之一是它以打印的方式显示文档 (WYSIWYG)。由于它不需要文字处理器来浏览文档,因此也方便了很多。您只需要他们的阅读器能够浏览文档。

    Perl 有一个模块可以让您将 HTML 文件转换为 PDF 文档。这是通过使用 HTML::HTMLDoc 模块完成的。

    安装

    您需要从 Easy Software Products 下载 HTMLDoc 产品。首先安装它,然后您可以继续从 CPAN 下载 HTML::HTMLDoc 模块。下载后,像安装典型 Perl 模块一样安装它。

    生成您的 PDF 文档

    首先要做的是使用该模块并创建 HTML::HTMLDoc 包的实例。

    然后您可以将完整的 HTML 文档传递给包或告诉它从 HTML 文件生成 PDF 文档。

    示例代码

    #!/usr/bin/perl
    use HTML::HTMLDoc;
    use strict;
    ####################################################
    # This script is distributed according to the terms of
    # the Perl Artistic License. Use at your own risk
    # © 2004 Philip L. Yuson
    ####################################################
    my $str = '
    <html>
    <body>
    <p><font size=14pt><b>HTML to PDF Document</b></font></p>
    <p>Let us see how this will work</p>
    <table border=1>
    <tr><td>This is a row in a table</td></tr>
    <tr><td>This is another row</td></tr>
    </table>
    <HR>
    copyright © 2004 Philip L. Yuson 
    </body>
    </html>';
    my $html = new HTML::HTMLDoc(); # Start instance
    $html->set_page_size('letter'); # set page size
    $html->set_bodyfont('Arial'); # set font
    $html->set_left_margin(1, 'in'); # set margin
    $html->set_html_content($str); # contents to convert
    my $pdf = $html->generate_pdf(); # generate document
    $pdf->to_file('article.pdf'); # save document
    

    将其保存为 pdf.pl 并通过启动命令行并输入以下内容来运行它:

    perl pdf.pl

    这应该会生成一个与此类似的 PDF 文件。

    生成 PDF 文件以供在网络上下载

    要生成可以下载的 PDF,而不是将其保存到文件中,您可以使用与上述相同的脚本,只是需要更改最后一行。但是,为了使我们的示例更有趣,我们还将在文档上添加日期和时间。所以脚本将如下所示:

    #!/usr/bin/perl
    use HTML::HTMLDoc;
    use Date::Calc;
    use strict;
    ####################################################
    # This script is distributed according to the terms of
    # the Perl Artistic License. Use at your own risk
    # © 2004 Philip L. Yuson
    ####################################################
    my @c = Date::Calc::Today_and_Now();
    my $str_temp;
    foreach (@c) {
     $str_temp .= sprintf("%02d:", $_);
    }
    my $str = "
    <html>
    <body>
    <p><font size=14pt><b>HTML to PDF Document</b></font></p>
    <p>Let us see how this will work</p>
    <table border=1>
    <tr><td>This is a row in a table</td></tr>
    <tr><td>This is another row</td></tr>
    </table>
    <HR>
    This document was generated:
    $str_temp
    copyright © 2004 Philip L. Yuson 
    </body>
    </html>";
    my $html = new HTML::HTMLDoc(); # Start instance
    $html->set_page_size('letter'); # set page size
    $html->set_bodyfont('Arial'); # set font
    $html->set_left_margin(1, 'in'); # set margin
    $html->set_html_content($str); # contents to convert
    my $pdf = $html->generate_pdf(); # generate document
    # Tell browser this is a a PDF document
    print "Content-Type: Application/pdf\n\n"; 
    $pdf->to_string(); # Send the document
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-26
      • 2013-08-10
      • 2010-10-10
      • 2014-09-22
      • 2013-07-04
      • 2013-02-11
      • 2015-09-24
      • 1970-01-01
      相关资源
      最近更新 更多