简体   繁体   English

magento FPDF错误:已经输出了一些数据,无法发送PDF文件

[英]magento FPDF error: Some data has already been output, can't send PDF file

i'm having the next problem: 我有下一个问题:

I try to see a pdf in one magento phtml this is my code: 我尝试在一个magento phtml中看到一个pdf文件,这是我的代码:

$fileName = Mage::getConfig()->getOptions()->getMediaDir()."/pdf/Gutschein-v2.pdf";

$pdf = new FPDI();
$pdf->addPage();
$pdf->setSourceFile($fileName);

$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm

$pdf->useTemplate($tplIdx, 10, 10, 100);

// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');

//ob_start();
$pdf->Output();
//ob_end_flush(); 

When i comment ob_start(); 当我评论ob_start(); i see on my screen the next error: FPDF error: Some data has already been output, can't send PDF file 我在屏幕上看到下一个错误:FPDF错误:已经输出了一些数据,无法发送PDF文件

When i don't comment that i see the normal pdf format but not the white page with my pdf, i tried to do that with pure php and everything was ok example: http://milton.bommelme.com/fpdf/pddf.php 当我不发表评论时,看到的是正常的pdf格式,但没有白页,我尝试使用纯PHP来做到这一点,并且一切正常,例如: http : //milton.bommelme.com/fpdf/pddf。的PHP

but with magento something are not gut, maybe i don't know how to load the pdf or something else. 但是用magento并不是什么东西,也许我不知道如何加载pdf或其他东西。 I'm very new with magento. 我对magento很陌生。

thank you. 谢谢。

The error "Some data has already been output, can't send PDF file" appears because Magento has already sent the header output. 出现错误“某些数据已经输出,无法发送PDF文件”,因为Magento已经发送了标题输出。 Look at http://php.net/manual/en/function.header.php for further informations. 有关更多信息,请访问http://php.net/manual/zh/function.header.php

So the Output() function of FPDF is also sending header informations and it declares the 'Content-Type' to 'application/pdf'. 因此,FPDF的Output()函数还将发送标头信息,并且将“ Content-Type”声明为“ application / pdf”。 The main problem here is that you cannot just put a PDF file between HTML tags. 这里的主要问题是您不能仅将HTML文件放在HTML标签之间。 PDF is an own format and needs a diffrent presantation mechanism than HTML. PDF是一种自己的格式,与HTML相比需要不同的呈现机制。 Like in the example link you gave the PDF file is embeded by the embed-tag with the right Content-Type declaration: 就像在示例链接中一样,您给PDF文件添加了带有正确Content-Type声明的embed-tag:

<embed width="100%" height="100%" name="plugin" src="http://milton.bommelme.com/fpdf/pddf.php" type="application/pdf">

There are also other ways to embed a PDF file in HTML: Recommended way to embed PDF in HTML? 还有其他将HTML文件嵌入HTML的方法: 建议将PDF嵌入HTML的方法?

EDIT: For example you can create two view actions. 编辑:例如,您可以创建两个视图操作。 The first one renders the HTML. 第一个呈现HTML。 Inside the HTML the embed-tag calls the second action which will generate the PDF. 在HTML内,embed标签会调用第二个操作,该操作将生成PDF。 I don't think this is the best solution,but it should work easily: 我认为这不是最好的解决方案,但它应该很容易工作:

class Foo_Pdf_Controller_SomeController extends Mage_Core_Controller_Front_Action
{

    public function viewAction()
    {
        // View stuff
    }

    public function getPdfAction()
    {
        // create pdf

        $fpdf->output();
    }
}

The embed-tags calls the getPdf() action: embed-tags调用getPdf()操作:

<embed width="100%" height="100%" name="plugin" src="url_to_getPdf_action" type="application/pdf">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用FPDF致命错误:有些数据已经输出,无法发送PDF文件? - Fatal error using FPDF:Some data has already been output, can't send PDF file? FPDF 错误:部分数据已经输出。 无法发送 PDF 文件 - FPDF error: some data has already been output. Can't send PDF file FPDF错误:已经输出了一些数据,无法发送PDF文件 - FPDF error: Some data has already been output, can't send PDF file FPDF错误:有些数据已经输出,无法在000webhost上发送PDF文件 - FPDF error: Some data has already been output, can't send PDF file on 000webhost 致命错误:未捕获异常:FPDF 错误:一些数据已经是 output,无法发送 PDF 文件(输出开始于 - Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file (output started at FPDF 错误:一些数据已经输出,无法发送 PDF - FPDF error: Some data has already been output, can't send PDF FPDF 错误:部分数据已输出,无法发送 PDF 文件。 尝试了一切,但没有任何帮助 - FPDF Error: Some data has already been output, can't send PDF file. Tried everything but nothing helped FPDF错误:已经输出了一些数据,无法发送PDF文件(输出从C:\\ xampp \\ htdocs \\ movie \\ form.php:15开始) - FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\movie\form.php:15) mPDF错误:部分数据已经输出到浏览器,无法发送PDF文件 - mPDF error: Some data has already been output to browser, can't send PDF file TCPDF ERROR:部分数据已经输出,无法发送PDF文件 - TCPDF ERROR: Some data has already been output, can't send PDF file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM