2016-09-14 64 views
0

我在PERL中工作,並且我有一個完美顯示的html結果頁面。我想添加一個按鈕,使得這個HTML頁面成爲下載到你的機器上的pdf。以下是生成html頁面的代碼。 fileFH在CGI代碼中生成。將HTML結果頁轉換爲PDF並下載CGI文件

sub searchResults { 
my $data = shift; 
my $file = "$OUTFILES/$data.html"; 

open(my $fileFH, '<', $file) or return "Can not find file\n"; 

print $cgi->header(); 

while (<$fileFH>) { 
    print qq($_); 
} 
close($fileFH); 
} 
+0

你能澄清,如果你需要幫助的代碼或者是您最感興趣的工具轉換成PDF? – mp3

+0

您可能還想考慮不推薦使用CGI.pm生成html代碼。 https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used – mp3

回答

2

如果你是開放爲轉換的外部程序/依賴我強烈建議http://wkhtmltopdf.org/

流量:

  1. 按鈕單擊
  2. wkhtmltopdf提供目標URL
  3. 輸出發送給用戶

您可能還想緩存輸出以減少轉換次數。

編輯 Perl也有一個包裝。

https://metacpan.org/release/PDF-WebKit

+0

And [PDF :: WebKit](https:// metacpan。 org/release/PDF-WebKit)是該程序的一個相當不錯的Perl包裝器。 –

+0

@DaveCross我甚至不知道存在。你讓我的生活更輕鬆 - 謝謝! – mp3

+0

它沒有做任何非常聰明的事情。這只是調用外部程序的一個非常薄的包裝。 –