通过Open2以编程方式从HTML中删除一行

时间:2016-09-15 22:23:29

标签: perl pdf-generation cgi

我有一个代码显示在两个不同的地方,一个是浏览器html页面,另一个是下载的PDF。在这个页面上有一行显示“使用打印按钮打印”但当然不在PDF模型上,因此我想在运行printFilePdf函数时将其删除。但是我无法添加(或者我不知道如何)条件执行方法中的HTML。

sub printHeader {
my ($outFH) = @_;

my ($sec, $min, $hour, $mday, $month, $year) = (localtime)[0, 1, 2, 3, 4, 5];
$month++;
$year += 1900;

my ($string) = scalar localtime(time());

print $outFH <<EOT;

<html>
<head>
    <title>THIS IS A TITLE</title>
</head>
<body>
<table width="640" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td class="bold">&nbsp;</td>
    </tr>
    <tr>
        <td class="bold">
            <div align="center" class="header">
                I want to keep this line $string<br>
            </div>
        </td>
    </tr>
    <tr>
        <td class="bold">&nbsp;</td>
    </tr>
    <tr>
        <td class="bold" style="color: red; text-align: center">
            I also what to keep this line.
        </td>
    </tr>
    <tr>
        <td class="bold" style="color: red; text-align: center">
            This line is not needed when the printFilePdf function is run.
        </td>
    </tr>
    </table>

   EOT

    print $outFH qq(<p align="center">&nbsp; </p>);

    print $outFH <<EOT;
        </td>
    </tr>
</table>

EOT

}

有没有这样做?就像在表格行中添加名称一样,在上面的方法中说出类似

的内容
if(!printFilePdf())
{
<tr>
    <td class="bold" style="color: red; text-align: center">
        This line is not needed when the printFilePdf function is run.
    </td>
</tr>
}

2 个答案:

答案 0 :(得分:0)

您可以查看来电者。如果调用者是printFilePdf,则搜索并替换以删除不需要的数据。

perldoc -f caller

作为旁注:如果你使用像HTML::Template这样的模板引擎,那就容易多了。在这种情况下,您可以将条件放在HTML中。

<TMPL_IF NAME="NON_PDF">
  Some text that only gets displayed if NON_PDF is true!
</TMPL_IF>

答案 1 :(得分:-1)

将HTML分成两部分:一种是两种格式共有的,一种是HTML特定的:

#!/usr/bin/perl
use warnings;
use strict;

sub printHeader {
    my ($outFH, $goes_to_html) = @_;

    print $outFH <<'__HTML__';
Here is the common text.
__HTML__

    print $outFH <<'__HTML__' if $goes_to_html;
This doesn't go to PDF.
__HTML__

}

print "To HTML:\n";
printHeader(*STDOUT, 1);

print "To PDF:\n";
printHeader(*STDOUT, 0);