【发布时间】:2019-04-19 05:59:59
【问题描述】:
我使用 FPDF/FPDI 将数据库中的 pdf 与图像合并为 PDF 的标题。正在寻找解决方案,但尚未找到解决方案,所以我在这里发布。
这是我的 viewpdf.php 代码:
<?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');
?>
它不会从数据库中加载 pdf。 “加载 PDF 文档失败。”
这是我的 upload.php,用于将 PDF 存储在数据库中。是我的 PDF 损坏了还是什么?我一直使用 PDO 进行数据库交互,但是这项工作实现了 mysqli,所以我需要遵循系统的流程。
<?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;
}
?>
我希望有人可以帮助我处理此代码。我试图解决 em 但仍然未能成功加载输出。我尝试在不使用 FPDF 的情况下查看 PDF,但仍然无法正常工作。我想知道我的代码在上传时是否会导致 BLOB 损坏?
【问题讨论】:
-
mysqli_error($virtual_con)在查询中返回了什么?文件的列确实是一个 blob 吗? -
['pdfblob'] 的列是 blob。
-
你回答了我问题的后半部分;第一部分呢?
-
dah solve ke belum ni?
-
@Fiido93 belum solve lagi...
标签: php mysqli file-upload blob fpdf