Output('yourpath/output.pdf', 'F');"; 3. Set "Output($ _SERVER['DOCUMENT_ROOT']"."/> Output('yourpath/output.pdf', 'F');"; 3. Set "Output($ _SERVER['DOCUMENT_ROOT']".">

Home  >  Article  >  Backend Development  >  What should I do if php cannot output the file?

What should I do if php cannot output the file?

藏色散人
藏色散人Original
2021-09-14 09:10:132461browse

Solution to the problem that php cannot output files: 1. Add "ob_clean();"; 2. Set "$pdf->Output('yourpath/output.pdf', 'F');"; 3. Set "Output($_SERVER['DOCUMENT_ROOT']".

What should I do if php cannot output the file?

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

What should I do if php cannot output the file?

Specific question:

php – TCPDF Error: Unable to create the output file

I am trying to generate pdf using a combination of TCPDF and FPDI. Here is my code.

require_once('../tcpdf/tcpdf.php');
require_once('../FPDI/fpdi.php');
$fileName = '../sample.pdf';
class PDF extends FPDI {
/**
 * "Remembers" the template id of the imported page
 */
var $_tplIdx;
var $numPages = 0;
/**
 * Draw an imported PDF logo on every page
 */
function Header() {
    global $fileName;
    if (is_null($this->_tplIdx)) {
        $this->setSourceFile($fileName);
        $this->_tplIdx = $this->importPage(1);
        $this->numPages = $this->setSourceFile($fileName);
    }
    $size = $this->useTemplate($this->_tplIdx);
}
function Footer() {
    // emtpy method body
}
}
// initiate PDF
$pdf = new PDF($fileName);
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// save file
$pdf->Output('output.pdf', 'F');

Here, the last line $pdf->output('output.pdf','F '); for saving the file. But it didn't work. When I only have $pdf-> Output(), it shows the pdf in the browser.

I tried $pdf->Output( 'output.pdf','D'); downloaded and it worked fine. It seems $pdf->output('output.pdf','F'); just didn't work, it showed the error TCPDF Error: Unable to create output file :output.pdf.

NOTE: There is no file permission issue

Can anyone point this out.

Solution:

Try putting ob_clean(); exactly at $pdf->output('output.pdf','F');

ob_clean();
// save file
$pdf->Output('output.pdf', 'F');

If that doesn't work. Than you need to set the path like this:

$pdf->Output('yourpath/output.pdf', 'F');

If you don’t know the absolute path, try this:

$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'output.pdf', 'F');

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if php cannot output the file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn