I am a novice and looking for a little guidance. Pulling PDF file stored as a BLOB (Varbinary(MAX)) without compression or encryption from a SQL server and trying to recreate the PDF file with the following code –
// Establish a connection to the database
$conn = odbc_connect($dsn, $user, $password);
if (!$conn) {
die("Connection failed: " . odbc_errormsg());
}
// SQL query to retrieve the BLOB data
$sql = "SELECT Data FROM [Attachment] WHERE AttachmentID = 3294713"; // ID will be changed to a variable when webform is created.
$AttachmentID = 3294713; //# will be changed to a variable when webform is created.
// Prepare and execute the SQL statement
$stmt = odbc_prepare($conn, $sql);
$result = odbc_execute($stmt, array($AttachmentID));
if ($result) {
// Fetch the BLOB data
$row = odbc_fetch_array($stmt);
if ($row) {
$blobData = $row['Data'];
// Write the BLOB data to a PDF file
$pdfFilePath = 'output.pdf';
//echo $blobData;
$file = "file.pdf";
//Load file content
$pdf_content = file_put_contents($file, $blobData);
//Specify that the content has PDF Mime Type
header("Content-Type: application/pdf");
//Display it
echo $pdf_content;
}
}
// Close the database connection
odbc_close($conn);
?>
When I echo $blobData, I see the data as –
%PDF-1.4 %���� 1 0 obj <> endobj 2 0 obj <>/Font<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/XObject<>>>/StructParents 0/Type/Page>> endobj 3 0 obj <> endobj 4 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> endobj 9 0 obj <>stream x��[�$����W�s ����p .....
I’ve searched high and low for too many hours without any success on how to display the binary stream as a PDF in a browser or Acrobat PDF reader. Any assistance or clues would be greatly appreciated.
Thanks in advance,
kcarey
I’ve tried converting the the binary data, adding multiple different headers, TCPDF, FPDF, FPDI, etc, etc.