Search code examples
phpfile-get-contents

file_get_contents by div id using tcpdf


I am a newbie and I am trying to upload an html page using TCPF and i used file_get_contents to get the html body , but still there are many buttons that i don't want to show up in my pdf file , got all the body but couldn't exclude those buttons , this is the php i am using (invoice is the id of the div that i want to print): (Edit : i've seen a similar problem , but it wasn't quite my case , the solutions didn't work for me) here's my code codepen.io/sescada/pen/LYWKxWm So while getting the PDF i always get the convert into pdf button with it , which i don't want to have in my PDF (i want to get all the html content except the button into the pdf ) I tried doing this with jspdf(it worked but looking for different ways of doing it xD!! ) and I tried for a while with DOM , didn't get how it works exactly so i stuck with TCPDF

<?php
if (isset($_POST['convert'])){
  require_once('tcpdf.php');
  $obj_pdf= new TCPDF ('p' ,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false);
  $obj_pdf->SetCreator(PDF_CREATOR);
  $obj_pdf->SetTitle("Essai");
  $obj_pdf->SetHeaderData('',0,PDF_HEADER_TITLE,PDF_HEADER_STRING);
  $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  $obj_pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
  $obj_pdf->SetHeaderMargin(1);
  $obj_pdf->SetFooterMargin(1);
  $obj_pdf->SetAutoPageBreak(TRUE, 1);
  $obj_pdf->AddPage();
  $content=file_get_contents('./cvtest.html',false,NULL);
  $first=explode("<div id='invoice'>",$content);
  $obj_pdf->writeHTML($first[0], true, false, true, false, '');
  $obj_pdf->lastPage();
  $obj_pdf->Output('try.pdf','I');
}
?>

Solution

  • You can simply hide unwanted elements using CSS. Check TCPDF examples on using CSS (e.g. this one).