如何在已加载的页面上生成和显示 TCPPDF pdf? [英] How to generate and display TCPDF pdf on a page that has already loaded?
问题描述
我正在尝试使用 TCPDF 动态生成 PDF 并将其显示在浏览器中.
I am trying to generate a PDF using TCPDF on the fly and display it in the browser.
我已经...
- 将 PDF 输出为下载
- 内联输出 PDF,没有任何 HTML.(使用
$pdf->Output('example_007.pdf', 'I');
我想做的是...
What I want to do is...
- 使用已打印的 HTML 内联输出 PDF,无需清除该 HTML.
自然我遇到了TCPD ERROR:一些数据已经输出,无法发送PDF文件
问题.我设法使用 ob 函数摆脱了这个错误,例如 ob_start
或 ob_flush
;但是,这会清除我现有的 HTML.
Naturally I ran into the TCPDF ERROR: Some data has already been output, can't send PDF file
issue. I managed to get rid of this error using the ob functions, such as ob_start
, or ob_flush
; however, this clears my existing HTML.
我尝试在通过 AJAX(纯 JavaScript)调用的 PHP 文件上输出 PDF;然而,我在这方面没有任何运气.什么都没有出现.
I've tried outputting the PDF on a PHP file that was called through AJAX (pure JavaScript); however, I have not had any luck with that end. Nothing ever appeared.
那么,当内容已经输出时,您将如何在浏览器中显示 PDF?
我或多或少正在寻找以下内容:
I am more or less looking for something like following:
我目前正在用一个测试页来做这件事,这是我的代码:...大部分来自 TCPDF 的示例.
I am currently doing this with a test page, here is my code: ... Most of it is from the TCPDF's examples.
//The following PHP block was tried but also failed as it wiped existing HTML
<?php
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test PDF</title>
</head>
<body>
<h1>Before PDF</h1>
<?php
error_Reporting(E_ALL);
date_default_timezone_set("America/New_York");
// Include the main TCPDF library (search for installation path).
require_once $_SERVER["DOCUMENT_ROOT"]."/my-site/TCPDF-master/tcpdf.php";
//ob_start() Tried this at some point... Didn't work
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
// Set some content to print
$html = "
<h1>Hello World!</h1>
";
// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
//ob_clean(); Various posts suggested various combinations of these methods
//ob_flush();
$pdf->Output('example_007.pdf', 'I');
//ob_end_flush();
//ob_end_clean();
?>
</body>
</html>
推荐答案
使用与内联显示相同的技术,您可以尝试使用 Iframe 加载 PDF 文件:
Using the same technique you used to display it inline you could try to load the PDF file using an Iframe:
<html>
<head>
</head>
<body>
<p>some content</p>
<iframe src="/path/to/your/pdf/script.php"></iframe>
</body>
</html>
这篇关于如何在已加载的页面上生成和显示 TCPPDF pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!