So I have seen other on the internet have had similar issues but the solutions don't really work for me. Also I've seen that these problems have been posted at least 4 years ago, so maybe some things have changed. Simply, I am trying to write a pdf using TCPDF and open that pdf in a new window. The catch is using AJAX since apparently, the array I sent to the server is not being sent in the success
section of the script and this the resulting pdf did not receive the appropriate data:
$(document).on("click", "#create_qr_codes_pdf", function(ev) {
ev.preventDefault();
var qr_array = [];
for (var i = 0; i < $("#qrcode_top img").length; i++) {
qr_array.push($('#code' +i+' img').attr('src'));
}
$.ajax({
url: 'server/qr_id_create.php?create_qr_codes_pdf',
type: 'POST',
data: {qr_array: qr_array},
success: function(data) {
window.open("C:/xampp/htdocs/*/server/qr_codes.pdf");
}
});
});
So as you can see I tried fetching the pdf from the server and opening it in a new tab, but apparently it's not allowed to open the local resource.
This is the php code used to create the pdf document:
if (isset($_GET['create_qr_codes_pdf'])) {
//ob_start();
if (isset($_POST['qr_array'])) {
$qr_images = $_POST['qr_array'];
require_once 'tcpdf/tcpdf.php';
//require_once('tcpdf/include/tcpdf_static.php');
require_once 'tcpdf/config/tcpdf_config.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('name');
$pdf->SetTitle('file');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 35, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, 35);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 9, '', true);
$pdf->AddPage();
foreach ($qr_images as $img) {
$img = preg_replace('/^[^,]*,\s*/', '', $img);
$img = base64_decode($img);
$pdf->Image('@'.$img);
$pdf->AddPage();
}
ob_end_clean();
$pdf->Output('qr_codes.pdf', 'F');
return true;
} else {
}
}
I have tried variations of $pdf->Output('qr_codes.pdf', 'F'); //I, F, D
. Ideally I would like to use the I
option to open in the browser, but this does not work at all. If I run the script, nothing is happening. Any ideas?