gpt4 book ai didi

php - 如何在已加载的页面上生成和显示 TCPDF pdf?

转载 作者:行者123 更新时间:2023-12-05 06:39:00 24 4
gpt4 key购买 nike

我正在尝试使用 TCPDF 即时生成 PDF 并将其显示在浏览器中。

我已经...

  • 将 PDF 作为下载输出
  • 输出没有任何 HTML 的内联 PDF。 (使用 $pdf->Output('example_007.pdf', 'I');

我想做的是...

  • 输出包含已打印 HTML 的内联 PDF,而不清除该 HTML。

当然我遇到了 TCPDF ERROR: Some data has already been output, can't send PDF file 问题。我设法使用 ob 函数消除了这个错误,例如 ob_startob_flush;但是,这会清除我现有的 HTML。

我尝试在通过 AJAX(纯 JavaScript)调用的 PHP 文件上输出 PDF;但是,我对此没有任何运气。什么都没有出现。

那么当内容已经输出时,如何在浏览器中显示 PDF?

我或多或少正在寻找如下内容:

enter image description here

我目前正在使用测试页执行此操作,这是我的代码:...大部分来自 TCPDF 的示例。

//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 文件:

<html>
<head>
</head>
<body>
<p>some content</p>
<iframe src="/path/to/your/pdf/script.php"></iframe>
</body>
</html>

关于php - 如何在已加载的页面上生成和显示 TCPDF pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45240807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com