我正在尝试生成一个包含图像和 highcharts 图像的 pdf 文件。这是我单击按钮时所做的:
('#button').click(function() {
var quizid = <?php echo json_encode($quizid); ?>;
var chart = Highcharts.charts[0];
canvg(document.getElementById('canvas'), chart.getSVG())
var canvas = document.getElementById("canvas");
var img = canvas.toDataURL("image/png");
//document.write('<img src="'+img+'"/>');
// AJAX CALL TO ACTION
$.ajax({
url: '/results/savepdf',
type:"POST",
data: {quizid: quizid, image: img},
success: function(data) {
console.log("ajax call succces");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
在我的行动中,我有:
public function savepdfAction(){
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
if(isset($_POST['quizid']))
$quizid = $_POST['quizid'];
if(isset($_POST['image']))
$image = $_POST['image'];
// SAVE THE PDF OR WORD
// INCLUDE TCPDF LIBRARY
require_once 'tcpdf/tcpdf.php';
try {
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 009');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 009', PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AddPage();
$pdf->setJPEGQuality(75);
$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');
$pdf->Image('@'.$imgdata);
$pdf->Image('/images/logo.png', 25, 40, 154, 25, 'PNG', 'http://www.surveyanyplace.com', '', true, 150, '', false, false, 1, false, false, false);
$horizontal_alignments = array('L', 'C', 'R');
$vertical_alignments = array('T', 'M', 'B');
$pdf->Output('example_009.pdf', 'I');
}
catch (Exception $e) {
die ('Application error: ' . $e->getMessage());
}
}
如您所见,我只想显示一个没有发送参数的 pdf 文件(只是为了测试)。
这不起作用....
当我在没有 ajax 调用的情况下尝试这个时,它工作正常......
我得到了这样的回应:
TCPDF ERROR: 一些数据已经输出,不能发送 PDF 文件
我搜索了错误并读到您可以通过ob_end_clean();
将$pdf->Output('example_009.pdf', 'I');
. 当我这样做时,我得到了另一个你可以在这里观看的回复。