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?
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.
$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.I hope this helped.
If your column in mysql pdfblob
type are blob
.
Then i suggest you to change the type to LONGBLOB
type.
Since BLOB
type in MySQL are only stores up to 64KB.
If still doesn't work then Change in the my.ini
max_allowed_packet=500M