2015-06-18 28 views
5

İstemci tarafında bir pdf oluşturmak için jsPDF kullanıyorum. Doc.save işleviyle ('filename.pdf') indirebilirim. Şimdi sunucuya kaydetmem gerekiyor, bu yüzden pdf verilerini .ajax() ile gönderiyorum ve bir PHP betiğiyle alıyorum fakat oluşturulan pdfURL'deki görüntüler görünmüyor (http://mydomain/tmp/test.pdf); sadece metni gösterir.ajax verisinden sunucuya pdf yükleme (jsPDF kullanarak)

Bana yardım edebilir misiniz lütfen?

Benim js kodu:

//doc.save('test.pdf'); WORKS WELL 
var pdf = doc.output(); 
$.ajax({ 
    method: "POST", 
    url: "inc/test.php", 
    data: {data: pdf}, 
}).done(function(data){ 
    console.log(data); 
}); 

PHP komut dosyası: Bu php betik proccess sonra oluşturulan pdf

<?php 
if(!empty($_POST['data'])){ 

    $data = $_POST['data']; 
    print_r($data); 

    file_put_contents("../tmp/test.pdf", $data); 
} else { 
    echo "No Data Sent"; 
} 
exit(); 
?> 

: http://control.edge-cdn.com.ar/tmp/test.pdf

Ve bununla oluşturulan doc.save() işlevi: http://control.edge-cdn.com.ar/repo/all.pdf Saygılar!

+0

Bu nedenle test.pdf pdf olarak açıldı, sadece bozuk görüntülü metin içeriyor mu? –

+0

Aynı bilgisayarda aynı yazılımla iki pdfs'yi (sunucudan ve js kullanılarak kaydedilen) açıyor musunuz? –

+0

-> doc.output() öğesinden js a pdf dosyası kullanarak üretebilir misiniz? veriler? Belki de bu hatanın sorumlusu budur (ve doc.save() belki de tamam) ... –

cevap

9

ÇÖZÜM: Ben ikili olarak pdf verilerini göndermeye çalışıyordu

. Sadece dize dizini kodlayın, gönderin ve bunu php'de çözün.

JS:

var pdf = btoa(doc.output()); 
    $.ajax({ 
     method: "POST", 
     url: "inc/test.php", 
     data: {data: pdf}, 
    }).done(function(data){ 
     console.log(data); 
    }); 

PHP:

if(!empty($_POST['data'])){ 
$data = base64_decode($_POST['data']); 
// print_r($data); 
file_put_contents("../tmp/test.pdf", $data); 
} else { 
echo "No Data Sent"; 
} 
exit(); 
0
var reader = new window.FileReader(); 
reader.readAsDataURL(doc.output("blob")); 
reader.onloadend = function() 
{ 
    ... 
    method: 'POST', 
    data: { 
     attachment: reader.result 
    } 
    ... 
} 
0

JS

var pdf =doc.output(); 
    var data = new FormData(); 
    data.append("data" , pdf); 
    var xhr = new XMLHttpRequest(); 
    xhr.open('post', 'inc/test.php', true); 
    xhr.send(data); 

PHP

if(!empty($_POST['data'])){ 
    $data = $_POST['data']; 
    $fname = "test.pdf"; 
    $file = fopen("test/pdf/" .$fname, 'r'); 
    fwrite($file, $data); 
    fclose($file); 
} else { 
    echo "No Data Sent"; 
} 
+0

kullanmayı denedim, dosya adını nasıl gönderebilirim? – daniel

İlgili konular