通过fpdf发布对象和pdf输出 [英] Post of an object and pdf output via fpdf

查看:115
本文介绍了通过fpdf发布对象和pdf输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好!我试图将一个大对象从JavaScript发送到php,以通过FPDF生成PDF输出. 到目前为止,您已经帮助我使用以下代码段发布了对象:

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

});

那行得通,我可以用php访问数据.但是,此方法似乎禁止pdf输出.即使我的php代码中除了fpdf的"Hello World"示例之外什么也没有,我也没有得到pdf文件:

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();
?>

我认为这可能是由于function(data){console.log(data)}造成的,但到目前为止,我还没有找到解决方法.

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.

编辑------------------

如果"post"调用是这样完成的:

If the "post" call is done like that:

post("output.php");

一切正常...

编辑------------------

编辑2 ------------------

如果您想通过jQuery创建pdf,这似乎是fpdf的普遍问题.一旦知道要寻找的内容,您实际上可以在SO中找到许多与此相关的帖子和​​问题. 但是,所有这些帖子然后建议不要使用jquery,而是通过"form"帖子发送值.好吧-这就是我要做的:

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();
}

但是,我的问题是,我有一个包含其他对象数组的对象( calcData ),但我不知道如何修改上面的代码以正确发布此数据. .可以使用jQuery post进行数据传输,但是pdf输出不起作用...如果有人知道解决任何一个问题的方法,将不胜感激!

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!

编辑2 ------------------

推荐答案

以防万一,其他任何人都需要发布多维数组/对象到php并创建pdf

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);
            }
        }
}

这可能不是最优雅的解决方案,如图所示,它仅适用于最多3维数组/对象,但我认为您可以理解,并且可以轻松扩展.使用此方法,您可以在不使用jQuery/Ajax的情况下将对象发布到php ,因此,如果您希望通过fpdf生成pdf ,则无需任何变通方法,您可以立即进行操作在用户端保存/显示pdf,而无需将数据保存在服务器上.

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!

这篇关于通过fpdf发布对象和pdf输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆