2016-09-15 37 views
2

我有一個代碼顯示在兩個不同的地方,一個是瀏覽器html頁面,另一個是下載的PDF。在這個頁面上有一行表示「使用打印按鈕打印」,但當然不在PDF模型上,所以我想在運行printFilePdf函數時將其刪除。然而,我不能添加(或者我不知道如何)一個條件做HTML中的方法。從HTML中刪除一行Pogramatically through Open2

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> 
} 
+1

這與您以前的問題有關嗎? (你從來沒有迴應過......)。如果是這樣,並且您正在使用該答案,則可以通過CSS處理。請告知這兩個問題。 – mp3

回答

-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); 
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>