Good morning! I am trying to send a large object from JavaScript to php, to generate a PDF output via FPDF. So far, you helped me to post the object with the following snippet:
jQuery.post('output.php', {
data: {
myObject:myObject
},
}, function(data) {
console.log(data);
});
That works, I can access the data in php. However, this method seems to prohibit a pdf output. Even if I have nothing in my php code than the 'Hello World' example of fpdf, I do not get a pdf-file:
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
I think this might be due to the function(data){console.log(data)}, but so far I haven't found out how to fix that.
EDIT ------------------
If the "post" call is done like that:
post("output.php");
everything works fine...
EDIT ------------------
EDIT 2 ------------------
It seems, that this is a general problem of fpdf if you want to create a pdf via jQuery. You can actually find many posts and questions regarding this in SO, once you know what to look for... However, all those posts then suggest not to use jquery but to transmit the values via a "form"-post. Well - this is what I did to begin with:
function post(path, calcdata, method) {
method = method || "post";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
form.setAttribute("target","_blank");
for(var key in calcdata) {
if(calcdata.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "CalculationData["+key+"]");
hiddenField.setAttribute("value", calcdata[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
My problem is however, that I have an object (calcData) which contains an array of other objects and I do not know, how I could adapt the code above to correctly post this data... with the jQuery post data transfer is possible but pdf output does not work... if anyone knows a solution for either problem, this would be much appreciated!
EDIT 2 ------------------