1

I need to convert url to pdf

I tried to use tcpdf but i get errors like: Notice: Undefined offset: 4 in /opt/lampp/htdocs/IBI/tcpdf/tcpdf.php on line 17218 TCPDF ERROR: Some data has already been output, can't send PDF file

I think that the problem is that the page have html and js and CSS

this is the code and the url that tried :

// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');

// 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 006');
$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.' 006', PDF_HEADER_STRING);

// 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 font
$pdf->SetFont('dejavusans', '', 10);

// add a page
$pdf->AddPage();
// test some inline CSS
$html = file_get_contents("http://dvns.me/mahmoud/IBI/");
$pdf->writeHTML($html, true, false, true, false, '');

//Close and output PDF document
$pdf->Output(time().'.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

3 Answers3

0

Try to insert ob_clean() before require() like

ob_clean()
// set document information
$pdf->SetCreator(PDF_CREATOR);

See the reference here and related issue here.

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56
0

I had achieved similar thing using the following library jsPDF

In view define the table you wish to print using ID for ex:

<table id="printTable" class='table table-striped' border="0">
Just_Do_It
  • 821
  • 7
  • 20
-1

Try to hide notice message with the following code :

error_reporting(E_ERROR | E_WARNING | E_PARSE);

This code is to insert at the beginning of the script

jmgross
  • 2,306
  • 1
  • 20
  • 24
  • i get this: Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/IBI/tcpdf/tcpdf.php on line 19515 Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/IBI/tcpdf/tcpdf.php on line 19515 Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/IBI/tcpdf/tcpdf.php on line 19515 Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/IBI/tcpdf/tcpdf.php on line 19515 TCPDF ERROR: Some data has already been output, can't send PDF file – Mahmoud Mahmid Apr 13 '15 at 14:00
  • It seem that your html is not valid to output in the pdf file. Try to replace your `$html`var with a simpler example like `Hello world` – jmgross Apr 13 '15 at 14:09
  • i tried before and it is working, i know that the problem is from the html outpou, but how can i fix it ? thanks – Mahmoud Mahmid Apr 13 '15 at 14:20
  • @huggilou please, var_dump the variable over which the foreach loops. – sitilge Apr 13 '15 at 14:22
  • I take a look to your page you want to convert to PDF. The problem is that you have some javascript script which will not be executed with `file_get_contents` function... You should use a PDF conversion on client side to keep graphs and etc – jmgross Apr 13 '15 at 14:25
  • I tried to add ob_end_clean();before the output, now i get results, but i see javascript code and the data but without design – Mahmoud Mahmid Apr 13 '15 at 14:26
  • @huggilou "You should use a PDF conversion on client side to keep graphs and etc" How can i make this thing ? are you have an example ? – Mahmoud Mahmid Apr 13 '15 at 14:27