如何在Postgresql中保存/读取mPDF生成的pdf

时间:2015-08-14 07:26:58

标签: php postgresql

我遵循PHP代码将生成的pdf保存为postgresql,如bytea:

session_start();

require_once(dirname(__FILE__) . '/../../../lib/mpdf/mpdf.php');
require_once(dirname(__FILE__) . '/../../../class/Postgre.php');

ob_start();
include(dirname(__FILE__) . '/../../FormVariables.php');
include(dirname(__FILE__) . '/../../../assets/templates/form.php');
$html = ob_get_contents();
ob_end_clean();

$mpdf = new mPDF('utf-8', 'A4');
$mpdf->WriteHTML($html);

$content = file_get_contents($mpdf->Output('','S'));

$postgre = new Postgre();
$postgre->connect();
$escaped = pg_escape_bytea($content);

$query = "
        INSERT INTO documents
        VALUES (
            '". $_SESSION['username'] ."',
            'form',
            1,
            '". $escaped."',
            '". $_SESSION['username'] ."_appraisal2015_1');";
$postgre->sendQuery($query);
$postgre->closeConnection();

并遵循PHP代码从db:

读取pdf文件
include(dirname(__FILE__) . '/../../../class/Postgre.php');

$postgre = new Postgre();
$postgre->connect();

$query = "SELECT file FROM documents WHERE type='form' AND owner_username='". $_POST['username'] ."';";
$postgre->sendQuery($query);
$fileBytea = pg_fetch_array($postgre->getQueryResult());
if($fileBytea){
    $file = $fileBytea['file'];
    header('Content-type: application/pdf');
    echo pg_unescape_bytea($file);
} else {
    $_SESSION['error']= "File does not exist.";
    header("Location: ../../../dashboard.php");
}

$postgre->closeConnection();

但是当我想从网页上读取这个文件时,我遇到了一个错误:“结果不是有效的文件”。

你能帮助我理解为什么它没有按照假设运作的可能原因吗?

1 个答案:

答案 0 :(得分:0)

有问题:

$content = file_get_contents($mpdf->Output('','S'));

$postgre = new Postgre();
$postgre->connect();
$escaped = pg_escape_bytea($content);

我不得不改变:

$content = file_get_contents($mpdf->Output('','S'));

对此:

$content = $mpdf->Output('','S');