关于mPDF字符串输出格式及前端读取转换方法的技术咨询
Let's break down your questions with clear explanations and practical examples:
1. What format is the string generated by $mpdf->Output('', "S")?
First, a quick correction: it’s not base68 (that’s likely a typo for base64). When you use the "S" parameter, mPDF returns the raw binary PDF data as a string—this is the exact byte-for-byte content of a PDF file, with no additional encoding applied. It’s the same data you’d find if you opened a PDF file in a hex editor.
2. How to read this string on the backend (PHP)?
Reading the string is straightforward—just assign the output to a variable, then you can manipulate it as needed:
// Initialize mPDF and build your content $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML('<h1>Sample PDF Content</h1><p>This is a test document.</p>'); // Capture the raw PDF binary string $pdfBinaryString = $mpdf->Output('', "S"); // Example: Save it to a local file file_put_contents('my-document.pdf', $pdfBinaryString);
3. How to handle this string in the frontend for viewing or download?
Directly passing raw binary strings to the frontend can cause encoding inconsistencies. Here are two reliable approaches:
Option A: Encode to base64 on the backend (simpler for frontend integration)
First, convert the raw binary string to base64 in PHP—this makes it safe to transmit via APIs or template variables:
$pdfBase64 = base64_encode($pdfBinaryString); // Pass this $pdfBase64 to your frontend (e.g., via API response or template)
Then use it in the frontend to view or download:
<!-- Embed PDF in an iframe --> <iframe src="data:application/pdf;base64,<?php echo $pdfBase64; ?>" width="100%" height="600px"></iframe> <!-- Create a direct download link --> <a href="data:application/pdf;base64,<?php echo $pdfBase64; ?>" download="my-document.pdf">Download PDF</a>
If using JavaScript (e.g., fetching from an API):
fetch('/api/get-pdf') .then(res => res.json()) .then(data => { // Add a viewable iframe const iframe = document.createElement('iframe'); iframe.src = `data:application/pdf;base64,${data.pdfBase64}`; iframe.style.width = '100%'; iframe.style.height = '600px'; document.body.appendChild(iframe); // Add a download button const downloadBtn = document.createElement('a'); downloadBtn.href = `data:application/pdf;base64,${data.pdfBase64}`; downloadBtn.download = 'my-document.pdf'; downloadBtn.textContent = 'Download PDF'; document.body.appendChild(downloadBtn); });
Option B: Send raw binary data and handle as a Blob in frontend
If you prefer to skip base64 encoding, send the raw binary data with proper headers from PHP:
header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="my-document.pdf"'); echo $pdfBinaryString; exit;
Then in frontend JavaScript, convert the response to a Blob for viewing/download:
fetch('/api/get-pdf-raw', { headers: { 'Accept': 'application/pdf' } }) .then(res => res.blob()) .then(blob => { const pdfUrl = URL.createObjectURL(blob); // Embed in iframe const iframe = document.createElement('iframe'); iframe.src = pdfUrl; iframe.style.width = '100%'; iframe.style.height = '600px'; document.body.appendChild(iframe); // Create download link const downloadBtn = document.createElement('a'); downloadBtn.href = pdfUrl; downloadBtn.download = 'my-document.pdf'; downloadBtn.textContent = 'Download PDF'; document.body.appendChild(downloadBtn); // Clean up memory after use iframe.onload = () => URL.revokeObjectURL(pdfUrl); downloadBtn.onclick = () => URL.revokeObjectURL(pdfUrl); });
内容的提问来源于stack exchange,提问作者Salar Houshvand

