0
我創建了一個運行在WAMP服務器上的Wordpress網站以及由GoDaddy託管的Linux服務器。在Linux服務器上用wkhtmltopdf創建PDF
我正在使用wkhtmltopdf來生成PDF文檔。
在WAMP服務器上一切都按預期工作,但在Godaddy + Linux服務器上沒有創建PDF,我只是得到一個空文檔。
我檢查了日誌,沒有任何內容被列出。
我在做linux環境的代碼時出錯或者服務器上出現了什麼問題?
<?php
require_once("../../../../wp-blog-header.php");
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['url']) && !empty($_GET['url'])) {
$post_id = url_to_postid($_GET['url']);
$post = $post_id ? get_post($post_id) : null;
if($post){
$post_url = get_permalink($post_id);
$html = file_get_contents($post_url);
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin
1 => array('pipe', 'w'), // stdout
2 => array('pipe', 'a'), // stderr <http://us3.php.net/manual/en/function.proc-open.php#97012>
);
$exe_path = (PHP_OS == 'WINNT') ? '"C:/wamp/bin/wkhtmltopdf/wkhtmltopdf.exe"' : ($_SERVER["DOCUMENT_ROOT"] . "/cgi-bin/whkhtmltopdf");
//$process = proc_open("$exe_path -q - -", $descriptorspec, $pipes);
$process = proc_open("$exe_path -q --print-media-type - -", $descriptorspec, $pipes);
// Send the HTML on stdin
fwrite($pipes[0], $html);
fclose($pipes[0]);
//stream_set_timeout($pipes[1], 1);
//stream_set_timeout($pipes[2], 1);
// Read the outputs
$pdf = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
// Close the process
fclose($pipes[1]);
$return_value = proc_close($process);
if($errors) {
header("HTTP/1.1 500 Internal Server Error");
header('Content-Type: text/plain');
echo "An error occurred during PDF generation:\r\n\r\n$errors";
} else {
$file_name = "$post->post_title.pdf";
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: ' . strlen($pdf));
header('Content-Disposition: inline; filename="' . $file_name . '";');
echo $pdf;
}
} else {
header("HTTP/1.1 500 Internal Server Error");
header('Content-Type: text/plain');
echo "An error occurred during PDF generation:\r\n\r\nUnrecognized post.";
}
}