我正在生成要生成PDF文件的項目。 我已經使用Google Chart API來生成不同的圖表。我正在使用TCPDF庫將它們轉換爲PDF,但我無法將這些生成的圖嵌入到PDF中。我認爲TCPDF不接受寫在腳本標記中的內容。我怎樣才能克服這個問題?使用TCPDF庫在PDF中嵌入圖表
回答
我遇到了同樣的問題,除了我正在使用FPDF。 在一天結束時,一個PDF文件包含靜態內容,所以不幸的是JavaScript不可能。最後我做什麼:
我準備圖表HTML +的Javascript像往常一樣,並把它寫在我的臨時目錄中的HTML文件。然後我使用PhantomJS(http://phantomjs.org/)創建頁面的屏幕截圖,然後將其包含在我的PDF中(或任何地方,真的)。
偉大的事情:它適用於任何本地HTML頁面。如果您只有一個URL,請使用file_get_contents()或cURL來檢索其內容並首先將其寫入本地HTML文件。
的膽量:
首先,下載並解壓phantomjs.exe到一個目錄應用程序可以訪問。我的例子使用版本1.9.8,我複製到我的應用程序根目錄中的lib/phantomjs。
我有一個不接受作爲參數的HTML文件的路徑和一組PhantomJS選項的功能。我建議將它添加到助手類。
function getHTMLImage($page, $options = array(), $js = null) {
// Prepare the PhantomJS directory that contains phantomjs.exe, e.g. lib or vendor
$phantomDir = $_SERVER['DOCUMENT_ROOT'] . '/lib/phantomjs/';
// Add the PhantomJS directory to the PATH
$origPath = str_replace('"', '', getenv('PATH'));
if (!in_array($phantomDir, explode('/', $origPath)))
putenv('PATH=' . $origPath . '/' . $phantomDir);
// PhantomJS requires a Javascript file to process the request. In case no Javascript processing file is given, use the default
if (is_null($js)) $js = $phantomDir . 'phantom.js';
// Prepare the PhantomJS call
$exec = 'phantomjs --ignore-ssl-errors=yes ' . $js . ' ' . escapeshellarg($page);
// Prepare the PhantomJS options, e.g. width and height
foreach ($options as $option) {
$exec .= ' ' . escapeshellarg($option);
}
// Call PhantomJS. To catch errors, call exec($exec . ' 2>&1', $output, $errorMsg);
exec($exec, $output);
// Decode and return the image data
return ($output ? base64_decode(reset($output)) : false);
}
JavaScript文件(我稱爲phantom.js並放置在同一個目錄中phantomjs.exe):
args = require('system').args;
page = require('webpage').create();
// In this case, I expect these indexes to be the width and height of the chart
if (typeof args[2] !== undefined) {
page.viewportSize = {
width: args[2],
height: (typeof args[3] === undefined ? args[2] : args[3])
};
}
page.open(args[1], function() {
var base64 = page.renderBase64('png');
console.log(base64);
phantom.exit();
});
這樣稱呼它:
// Make sure $width and $height are set, or exclude them altogether
$output = getHTMLImage($htmlPath, array($width, $height));
// Example output, you want to save the image and include it in your PDF file
header('Content-Type: image/png');
exit($output);
getHtmlImage()的代碼顯示的錯誤是: - –
調用未定義的函數addpage(); –
我的不好,我是指一個本地定義的函數。我編輯了我的答案,請再試一次。 – RuubW
- 1. 如何使用TCPDF將字體嵌入到PDF中?
- 2. 如何使用TCPDF PDF使用Arial字體不嵌入
- 3. 在tcpdf中嵌入SVG XML
- 4. 使用tcpdf在PHP中使用tcpdf的svg轉換爲pdf
- 5. 在使用itextsharp生成的PDF中嵌入圖像sharepoint庫
- 6. 使用TCPDF生成PDF - 無法提取嵌入字體
- 7. Ajax調用後在嵌入標籤中使用TCPDF創建預覽pdf文件
- 8. 使用TCPDF在ASP.NET MVC中生成PDF
- 9. 使用TCPDF從PDF創建圖像
- 10. 在TCPDF庫中寫入HTML
- 11. TCPDF在現有PDF中插入頁面
- 12. 使用tcpdf將mathml下載到PDF中
- 13. 用TCPDF將空間寫入PDF文件
- 14. 如何使用數據庫中的TCPDF創建pdf
- 15. 在pdf中嵌入的pdf閱讀pdf
- 16. 在PDF中嵌入PDF(PDF收錄:))
- 17. 在pdf中嵌入背景圖片
- 18. 如何在pdf中嵌入圖像?
- 19. 如何使用TCPDF生成pdf中的表格中心?
- 20. 嵌入式動態PDF圖表
- 21. 如何使用cfpdfform在PDF中嵌入圖像
- 22. 如何使用JPod在PDF中嵌入圖像?
- 23. 使用TCPDF生成PDF文件中的圖形
- 24. 將條碼嵌入到C#PDF庫中
- 25. 在PDF中嵌入SVG(使用JS將SVG導出爲PDF)
- 26. 在網頁上使用PDF.js嵌入PDF
- 27. pdf嵌入在emacs中?
- 28. 在pdf中嵌入視頻
- 29. 在IE10中嵌入PDF
- 30. 在HTML中嵌入PDF
顯示您腳本,請。 – Masiorama