Sending post PDF data using hidden input

790 views Asked by At

I have an app which finally creates pdf from img using js. PDF data is inserted in hidden input. Next I send pdf to email address.

I have a problem with sending hidden input with pdf data via post.

While using free server it all works fine but on destination server hidden field is not received. Hidden field with just text works fine.

I checked max file upload data (62M).

1

There are 1 answers

0
Łukasz Klapiszewski On

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

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

<?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="[email protected]";    
$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); 

?>