使用隐藏输入发送PDF数据

时间:2015-06-15 18:30:07

标签: php pdf post input hidden

我有一个应用程序,最终使用js从img创建pdf。 PDF数据插入隐藏输入中。接下来,我将pdf发送到电子邮件地址。

我通过post发送带有pdf数据的隐藏输入有问题。

使用免费服务器时,一切正常,但未收到目标服务器隐藏字段。只有文字的隐藏字段工作正常。

我检查了最大文件上传数据(62M)。

1 个答案:

答案 0 :(得分:0)

HTML code:

    <div id="formPopUpDiv" style="display:none">    
    <form method="post" accept-charset="utf-8" name="form1">            
        <div class="formDiv">
            <div class="orderData">
                <label>Imię i nazwisko</label>
                <input type="text" id="nameTxt" name="name"/>
                <label>Numer zamówienia</label>
                <input type="text" id="orderNoTxt" name="orderNo"/>
                <label>AdresEmail</label>
                <input type="text" id="emailTxt" name="email_from"/>
                <input name="hidden_data" id='hidden_data' type="hidden"/>      

            </div>
            <div class="orderButtons">
                <input type="button" value='Wyślij' onclick="popupForm('formPopUpDiv', 300, 150, true)" id="sendButton" class="sendButton"/>
                <input type="button" value='Odrzuć' onclick="hideForm('formPopUpDiv')" id="sendButton" class="sendButtonHide"/>
            </div>
        </div>
    </form>

</div>

JS代码:

if(isValid){            
        blanket_size(windowname);
        form_window_pos(windowname, width, height);
        toggle('blanket');
        toggle(windowname);

        drawDestImage(destinationCanvasArray, false);   

        var destCanvas = document.getElementById("destCanvasElement");

        var dataPDF = destCanvas.toDataURL("image/jpeg");
        var pdf = new jsPDF();

        pdf.addImage(dataPDF, 'JPEG', 0, 0, 210, 210);//210 is A4 width in mm
        //pdf.save("download.pdf");

        var pdfUrl = pdf.getDataURLString();

        document.getElementById('hidden_data').value = pdfUrl;
        //console.log(document.getElementById('hidden_data').value);
        var fd = new FormData(document.forms["form1"]);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'upload_data.php', true);

        xhr.upload.onprogress = function(e) {
            if (e.lengthComputable) {
                var percentComplete = (e.loaded / e.total) * 100;
                //console.log(percentComplete + '% uploaded');
            }
        };

        xhr.onload = function() {

        };
        xhr.send(fd);
    }

PHP代码:

<?php
$upload_dir = "resultImg/";
$img = $_POST['hidden_data'];
$img = str_replace('data:application/pdf;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . mktime() . ".pdf";
$success = file_put_contents($file, $data);
message $success ? $file : 'Unable to save the file.';

srand((double)microtime()*1000000); 
$znacznik = md5(uniqid(rand())); 


$mojadres="pawel@sg2.pl";    
$tytul=$_POST['name']." ".$_POST['orderNo']; 
$tresclistu ="Podkładki"; 
$nadawca =$_POST['email_from']; 
$nadawca_mail=$_POST['email_from']; 

$naglowki = "From: $nadawca <$nadawca_mail>\n"; 
$naglowki .= "MIME-Version: 1.0\n"; 
$naglowki .= "Content-Type: multipart/mixed;\n"; 
$naglowki .= "\tboundary=\"___$znacznik==\""; 

$x="--___$znacznik==\n"; 
$x .="Content-Type: text/plain; charset=\"iso-8859-2\"\n"; 
$x .="Content-Transfer-Encoding: 8bit\n"; 
$x .="\n$tresclistu\n"; 


$plik_nazwa_tmp = $file["tmp_name"]; 
$plik_nazwa_oryginalna = $file["name"]; ;
$plik_wielkosc = $file["size"]; 

$x .="--___$znacznik==\n"; 
$x .="Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n"; 
$x .="Content-Disposition: attachment;\n"; 
$x .=" filename=\"$tytul\".pdf\n"; 
$x .="Content-Transfer-Encoding: base64\n\n"; 


$x .= chunk_split(base64_encode($data)); 
$x .="--___$znacznik==--\n"; 


mail($mojadres,$tytul,$x,$naglowki); 

?>