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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建