ProducePDF.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3. use \App\Models\BlogModel;
  4. use TCPDF;
  5. use Dompdf\Dompdf;
  6. //use CodeIgniter\I18n\Time;
  7. //just testing
  8. class ProducePDF extends BaseController
  9. {
  10. protected $myDate;
  11. protected $myTime;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->myTime = parent::getTime();
  16. $this->myDate= date("d/m/Y",$this->myTime);
  17. }
  18. public function getPdf($slug)
  19. {
  20. echo "slug is ".$slug;
  21. /***
  22. *
  23. * strip_tags("Hello <b><i>world!</i></b>","<b>");
  24. * above allows <br>
  25. *
  26. * as of PHP 7.4.0 the line above can be written as:
  27. * echo strip_tags($text, ['p', 'a']);
  28. *
  29. *
  30. *
  31. */
  32. $handle= new BlogModel();
  33. $result= $handle->getArticle($slug);
  34. $mytitle= $result["slug"];
  35. $mystring= $result["article"];
  36. $mystring = $mytitle."<br><br>".$mystring;
  37. $theImage = $result["image"];
  38. $myStringStripTags= strip_tags($mystring, ['p', 'a', 'h4', 'h1','h3','h2']);
  39. echo "start of using TCPDF";
  40. $html2pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  41. //TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  42. $html2pdf->setTitle("hello world");
  43. $html2pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  44. $html2pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  45. $html2pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  46. $html2pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
  47. $html2pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  48. $html2pdf->setFontSubsetting(true);
  49. //$html2pdf->SetFont('dejavusans', '', 12, '', true);
  50. $html2pdf-> AddPage('P',"A4");
  51. $html2pdf->Image(base_url().'/blogImages/'.$theImage, '', 45, 50, 50, 'JPG', false);
  52. //above works but need space
  53. // $html2pdf->writeHTMLCell(0, 0, '', '', $mystring, 0, 1, 0, true, '', true);
  54. $html2pdf->writeHTMLCell($w=0, $h=0, $x='', $y='100', $mystring, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
  55. ob_end_clean();
  56. $html2pdf->Output($mytitle.".pdf", 'D');
  57. }
  58. public function index()
  59. {
  60. }
  61. public function getPdf2($slug)
  62. {
  63. $handle= new BlogModel();
  64. $result= $handle->getArticle($slug);
  65. $mytitle= $result["slug"];
  66. $mystring= $result["article"];
  67. $theImage = $result["image"];
  68. $imgSrc= "<img src=". base_url('blogImages/'.$theImage). ">";
  69. $mystring = $mytitle."<br><br>".$mystring;
  70. $dompdf = new Dompdf();
  71. // load html
  72. $dompdf->loadHtml($mystring);
  73. // set paper size and orientation
  74. $dompdf->setPaper('A4', 'portrait');
  75. // render html as pdf
  76. $dompdf->render();
  77. $dompdf ->stream($slug.".pdf");
  78. exit();
  79. // output the pdf to browser
  80. echo "process has reached this stage";
  81. }
  82. }