Post of an object and pdf output via fpdf

吃可爱长大的小学妹 提交于 2019-12-24 16:35:17

问题


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 ------------------


回答1:


Just in case, anyone else will need to post a multidimensional array/object to php and create a pdf from it, here is my solution:

for(var key in calcdat) {
        if(calcdat.hasOwnProperty(key)) {
            if(typeof(calcdat[key]) == 'object' || typeof(calcdat[key]) == 'array'){
                for(var key2 in calcdat[key]){
                    if(calcdat[key].hasOwnProperty(key2)){
                        if(typeof(calcdat[key][key2]) == 'object' || typeof(calcdat[key][key2]) == 'array'){
                            for(var key3 in calcdat[key][key2]){
                                if(calcdat[key][key2].hasOwnProperty(key3)){
                                    var hiddenField = document.createElement("input");
                                    hiddenField.setAttribute("type", "hidden");
                                    hiddenField.setAttribute("Name", "CalculationData[" + key + "][" + key2 + "][" + key3 + "]");
                                    hiddenField.setAttribute("value", calcdat[key][key2][key3]);

                                    form.appendChild(hiddenField);                                      
                                }
                            }
                        }else{
                            var hiddenField = document.createElement("input");
                            hiddenField.setAttribute("type", "hidden");
                            hiddenField.setAttribute("Name", "CalculationData[" + key + "][" + key2 + "]");
                            hiddenField.setAttribute("value", calcdat[key][key2]);

                            form.appendChild(hiddenField);
                        }
                    }
                }
            } else {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", "CalculationData["+key+"]");
                hiddenField.setAttribute("value", calcdat[key]);

                form.appendChild(hiddenField);
            }
        }
}

This might not be the most elegant solution, and as displayed it only works for up to 3-dimensional arrays/objects but I think you can get the idea and it is easily extendible. This method lets you post your object to php without jQuery/Ajax and thus if you want to generate a pdf via fpdf, you can do so without any workaround and you can immediately save / display the pdf on user-side without saving the data on the server.

Hope that helps you to save time, it took me quite a while to locate the problems I had with this (simple) task... Cheers!




回答2:


I might give you solution for mpdf if you'd like that:

include __DIR__.'..../mpdf.php';
$mpdf=new mPDF(); 

$mpdf->WriteHTML($HTML_CONTENT);
$mpdf->Output('filename.pdf','F');

so, problem may be similar with fpdf, do you see the arguments of output() function?



来源:https://stackoverflow.com/questions/47547758/post-of-an-object-and-pdf-output-via-fpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!