2016-04-27 90 views
0

嘿傢伙我試圖使用html2pdf生成pdf文件,但我無法成功使其工作,因爲我得到一個不可讀的內容 所以基本上我有一個簡單的PHP頁面,生成一個PDF從另一個側面,我有我的服務文件使用html2pdf with angularjs

$content = ob_get_clean(); 

require_once(dirname(__FILE__).'/../vendor/autoload.php'); 
try 
{ 
    $html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', 0); 
    $html2pdf->writeHTML($content, isset($_GET['vuehtml'])); 
    $html2pdf->createIndex('Sommaire', 25, 12, false, true, 1); 
    $html2pdf->Output('bookmark.pdf'); 
} 
catch(HTML2PDF_exception $e) { 
    echo $e; 
    exit; 
} 

,他的一些數據發送給它,並得到迴文件是這樣的

this.generatePDF = function (commande) { 
    var deferred = $q.defer(); 
    $http({ 
     method: 'POST', 
     //responseType: 'arraybuffer', 
     url: 'vendor/modules/html2pdf/examples/bookmark.php', 
     timeout: 15000, 
     data: $.param({'data': commande}), 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     //headers: {'Content-Type': 'application/pdf'} 
     //header :{"Content-Disposition": "attachment; filename=sample.pdf"} 
    }).then(function successCallback(response) { 
     debugger; 
     deferred.resolve(response.data); 
    }, function errorCallback(response) { 
      deferred.resolve(response.statusText); 
    }); 
    return deferred.promise; 
}; 

爲這是控制器端的最後一部分時,用戶presse生成我打電話給我的服務並將數據綁定到它然後得到整個東西回來成功後,並將其寫入到一個新的窗口的內容

var popup = $window.open('', 'TEST', 'width=500,height=900'); 

ServiceCommande.generatePDF($scope.commande).then(function (data) { 
    popup.document.write(data); 
}); 

的東西是得到一些奇怪的東西,而不是說我送 strange behavior pdf format

感謝您的PDF^^

+0

這是一個PDF看起來像文本。這不是一個可讀的格式。你可以 - 爲了測試目的 - 把奇怪的東西保存在一個文件結尾* .pdf *並嘗試打開它。你想用你生成的pdf做什麼?如果你想在彈出窗口中顯示它,我想你需要先將它保存爲pdf,然後才能將它加載到你的html中。 – kabaehr

+0

感謝您的快速回復,是啊,這正是我想要它做的我已經將文件保存爲PDF格式,所以而不是發送整個PDF文本我只需要發送文件的當前位置打開是這樣嗎? –

回答

0

嘗試使用PhantomJS`。它已經得到了CSS元素的廣泛支持。

安裝它,並將可執行文件放在系統環境PATH中。 創建一個文件index.js。這個文件的內容將是:

//create page 
var page= require('webpage').create(); 
var system = require('system'); 

page.paperSize = { 
    format: 'A4', 
    orientation: 'portrait' 
} 

//check for number of parameters 
if (system.args.length < 3) { 
    console.log('Usage: phantomjs index.js <web page URL> <ouptut path/filename>'); 
    phantom.exit(); 
} 

page.open(system.args[1], function (status) { 
    console.log("Status: " + status); 
    if (status === "success") { 
     page.render(system.args[2]); 
    } 
    else { 
     console.log("Failed") 
    } 
phantom.exit(); 
}); 

立即抓取任何網頁鏈接後,將其轉換成PDF,發出命令爲:

phantomjs index.js index.html index.pdf