1

I have to add a watermark to my existing pdf file. I use fpdf and fpdi libraries.

My code is:

<?php
ob_start();
require('fpdf/fpdf.php');
require 'fpdi/fpdi.php';
$pdf = new FPDI();
$pdf->setSourceFile("Rabochaya_tetrad.pdf");
$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 
$pdf->SetFont('Arial');
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(58, 45);
$your_dynamic_content="file watermark";
$pdf->Write(0,$your_dynamic_content);
$pdf->Output();
ob_end_flush();
?>

When I try to perform it, I get an error:

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

I tried all advices from FPDF error: Some data has already been output, can't send PDF, but it didn't help me.

Do you have any ideas about these issues?

RamenChef
  • 5,557
  • 11
  • 31
  • 43

2 Answers2

1

You have to move the line ob_start(); before the line $pdf->Output().

The error is thrown because one (or more) function prints something and your PDF file is corrupted when displayed.

Spomky-Labs
  • 15,473
  • 5
  • 40
  • 64
0

require('fpdf/fpdf.php');

should be like this

require 'fpdf/fpdf.php';

refer to http://php.net/manual/en/function.require.php

FullStack
  • 198
  • 7
  • 16