dongtang6681 2018-11-16 03:56
浏览 254

无法使用FPDF显示PDF BLOB

Im using FPDF/FPDI to merge the pdf from database with image as a header of the PDF. Searching for solution but no solution found yet so I post it here.

Here is my viewpdf.php code:

  <?php
/**
 * Simply import all pages and different bounding boxes from different PDF documents.
 */
use setasign\Fpdi;
use setasign\fpdf;
require_once 'config/config.php';
require_once 'vendor/autoload.php';
require_once 'vendor/setasign/fpdf/fpdf.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(2);
date_default_timezone_set('UTC');
$start = microtime(true);
$pdf = new Fpdi\Fpdi();
//$pdf = new Fpdi\TcpdfFpdi('L', 'mm', 'A3');
if ($pdf instanceof \TCPDF) {
    $pdf->SetProtection(['print'], '', 'owner');
    $pdf->setPrintHeader(true);
    $pdf->Image('logo-small.png',50,5,100);
    $pdf->setPrintFooter(false);
}
$certid= $_GET['id'];
$sql="SELECT pdfblob from subject WHERE  certid = '".$certid."' ";
    $resultc = $virtual_con->query($sql);
 while($row=mysqli_fetch_assoc($resultc))   {
   header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); 
    header("Content-Type:application/pdf");
    header("Content-Transfer-Encoding: binary");
    $a = (base64_decode($row["pdfblob"]));
$files = [$a];
foreach ($files as $file) {
    $pageCount = $pdf->setSourceFile($file);
    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        $pdf->AddPage();
        $pageId = $pdf->importPage($pageNo, '/MediaBox');
        $pageId = $pdf->importPage($pageNo, Fpdi\PdfReader\PageBoundaries::ART_BOX);
        $s = $pdf->useTemplate($pageId, 10, 10, 200);
    }
}
$file = uniqid().'.pdf';
$pdf->Output('I', 'simple.pdf');
}
//$pdf->Output('output/'.$file, 'I');
?>

It doesn't load the pdf from the database. "Failed to load PDF document."

Here is my upload.php to store the PDF in database. Is it my PDF is corrupt or something? I'm always using PDO for database interaction but this work implement mysqli so I need to follow the flow of the system.

<?php
include('config/config.php');
session_start();
$certid=$_POST['certid'];
$vendorid=$_POST['vendorid'];
$sid=$_POST['subjectid'];
$target_file = $_FILES["fileToUpload"]["name"];
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$uploadOk = 1;
if (isset($_POST['submit'])) {
    $data = mysqli_real_escape_string($virtual_con,$_FILES['fileToUpload']['tmp_name']);
    $data = file_get_contents($data);
    $data = base64_encode($data);
    $uploadOk = 1;
    if (isset($data)) {
        $sql="UPDATE `subject` SET `pdfblob`='".$data."' WHERE (`Subjectid`='".$sid."')";
        $result=mysqli_query($virtual_con,$sql);
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        $to="subjects.php?subjectid=".$sid."&certid=".$certid."&vendori=".$vendorid;
        gotoInterface($to);
    }
    else{
        echo 'Fail to upload file';
    }
}
if ($imageFileType !="pdf" ) {
    echo "Sorry, PDF is allowed.";
    $uploadOk = 0;
}
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
?>

I hope somebody could help me working on this code. I tried to solve em but still failed to succeed loading the output. I try to view the PDF without using FPDF but yet still not working. I'm wondering is my code make the BLOB corrupt while uploading?

  • 写回答

2条回答 默认 最新

  • doulin8374 2018-11-16 04:29
    关注

    I'm making this as an answer instead of a comment because it will be too long for a comment. Follow these steps to determine where it's going wrong.

    1. Your problem MIGHT be that your line $data = mysqli_real_escape_string($virtual_con,$_FILES['fileToUpload']['tmp_name']); might be coorrupting the file. Try saving a known good pdf to the database from local storage and not using the upload file process and then try to decode it again. If you can turn that back in to a pdf, then the your upload is corrupting it. Otherwise, skip to step 2.
    2. Encode a file and save the output before uploading to the database (in a text editor). Then, query the database for that file and before you decode it, paste that string in to a text editor also and compare the two strings. If they are different, you know your database is doing something to the file. If they're the same, I don't know what else to tell you.

    I hope this helped.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图