iT邦幫忙

0

[php,pdf]wkhtmltopdf不同版本的用法改寫(unpatched QT)

舜~ 2021-02-17 23:15:232348 瀏覽

各位前輩們請教一下,我用php透過wkhtmltopdf套件來產生pdf

我開發用的電腦是win10 64bit,PHP 7,wkhtmltopdf版本0.12.6
正式機是Ubuntu 20.04.1 LTS,PHP 7,wkhtmltopdf版本0.12.5
(原本想用docker模擬開發環境,但意外的還沒搞定@@ 先擱著有空再回頭處理...)

再開發時產生pdf沒問題
但是上傳到正式機後,
執行過程不會出錯,
但產生的pdf就是開不了,

先上code


/**
* 將網址轉成pdf,透過wkHtmlToPdf工具
*
* @param string $url 內容網址
* @param string $path 存檔路徑
* @param string $coverUrl 封面網址
* @return void
*/
public static function url2pdf($url, $path, $coverUrl)
{
    $wkhtmltopdfExe = 'xvfb-run -a wkhtmltopdf '; //xvfb-run -a 
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'){
        $wkhtmltopdfExe = 'C:\VisualHosts\tool\wkhtmltopdf.exe ';
    }
    
    $command = "$wkhtmltopdfExe"
    . " --cookie PHPSESSID " . (in_array('PHPSESSID', $_COOKIE)? $_COOKIE['PHPSESSID'] : 'fapdce734t1f6475tbk90cjnt5')
    . ' --page-size A4' // 設定紙張大小
    . ' --disable-smart-shrinking' // 禁止縮放
    . ' --image-dpi 150 ' //指定尺寸的图片(默认值是 600)
    . ' --lowquality ' // 低質量輸出,讓檔案小一點(畫質可能差一點)
    . ' --load-error-handling ignore'// <handler> 指定当页面加载失败后的动作,可以指定为:abort(中止)、ignore(忽略)、skip(跳过);(默认值是:abort)
    . ' --enable-local-file-access  --allow  ' // 同意可使用本地文件,避免出現Warning: Blocked access to file

    // ==== 封面 ====
    . "  cover \"$coverUrl\""

    // ==== 目錄 ====
    . " toc "
    . ' --xsl-style-sheet '. __DIR__ . DIRECTORY_SEPARATOR .'pdfTocStyle.xsl' // 目錄樣式,主要為了調整字體,不然直接使用"--toc-header-text '目錄標題'"就可以了

    // ==== 內容 ====
    . " \"$url\" " // 來源
    . " --footer-center [sitepage]" // 頁碼 ([sitepage]:該來源的當前頁數 [page]:輸出的當前頁數 [topage]:輸出的總頁數)

    // ==== 輸出 ====
    . " \"$path\""; // 存放位置

    $printMessage = shell_exec($command);
    echo $printMessage;
}

如果不加上xvfb-run的話產生的pdf我沒有程式可以正常的打開,
加上的話chrome開不了,但可用pdf-XChange工具可以開啟
開啟的錯誤內容如下

The switch --disable-smart-shrinking, is not support using unpatched qt, and will be ignored.

The switch --image-dpi, is not support using unpatched qt, and will be ignored.

The switch --xsl-style-sheet, is not support using unpatched qt, and will be ignored.

The switch --footer-center, is not support using unpatched qt, and will be ignored.

QStandardPaths :
XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime—www—data'

Error: This version of wkhtmltopdf is build against an unpatched version of QT, and does not support more then one input document. Exit with code 1, due to unknown error.

錯誤訊息簡單說就是此版本的wkhtmltopdf是針對未修補的QT版本構建的,並且不支持一個以上的輸入文檔。

--disable-smart-shrinking、--image-dpi 被忽略的話檔案會挺肥的,有沒有別的方式瘦身?
--xsl-style-sheet 被忽略就沒有目錄了,不過可以用--toc-header-text '目錄標題' 替代(還沒試),但樣式...乾脆不要目錄免得被噴的更慘...
--footer-center 被忽略就沒有頁尾的頁碼了,鐵定被噴

不支援超過1個的輸入...捨棄目錄,封面可以跟內容做一起,這樣就單一輸入,但用戶要求封面不要有頁碼,頁碼從第二頁開始算第一頁...沒試成功,可能不支援,所以封面跟內容一定要分開,然後套footer-center....

請問我wkhtmltopdf的語法要怎麼修改比較好?
或是大家怎麼產生pdf的??

有用過dompdf...坑XD
最新版中文亂碼樣式大亂不知怎麼解決只好回頭繼續用舊版(支援php5.5的最後一版)
但舊版只支援部分html4 + 部分css2,
且動態表格若字數太多樣式會有異常,遇上動態大表格則會異常的緩慢,還有不支援頁碼...用戶一直噴...

froce iT邦大師 1 級 ‧ 2021-02-17 23:59:22 檢舉
ubuntu的repo的版本?
wkhtmltopdf建議去他的官網下來安裝,不要用官方repo,我記得會有問題。

wkhtmltopdf官網的deb有headless模式,不需要xvfb-run
舜~ iT邦高手 1 級 ‧ 2021-02-19 09:38:28 檢舉
伺服器環境不是我架的...
若環境無法變更的話,要怎麼處理?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答