perec.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. session_start();
  3. //include autoloader
  4. require_once 'dompdf/autoload.inc.php';
  5. // reference the Dompdf namespace
  6. use Dompdf\Dompdf;
  7. // instantiate and use the dompdf class
  8. $dompdf = new Dompdf();
  9. $dompdf->set_option('defaultFont', 'Helvetica');
  10. // execute shell python commande
  11. // change the command with the name of your script
  12. $command = escapeshellcmd('python scripts/perec.py');
  13. $output = shell_exec($command);
  14. //get random word for the cover
  15. $word = explode("<p>", $output);
  16. $randomWord = $word[rand(0, count($word))];
  17. $dompdf->loadHtml(
  18. '<html>
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  21. <title>PEREC</title>
  22. <link rel="stylesheet" type="text/css" href="style.css" />
  23. </head>
  24. <body class="toPrint">
  25. <header>
  26. <!-- Change the cover here -->
  27. <div class="randomWord">'.$randomWord.'</div>
  28. <h1>Frankenstein</h1>
  29. <h2>Marie Shelley</h2>
  30. </header>
  31. <main><p>'.
  32. $output.
  33. '</p></main>
  34. </body></html>'
  35. );
  36. //(Optional) Setup the paper size and orientation
  37. $dompdf->setPaper('A6', 'portrait');
  38. // Render the HTML as PDF
  39. $dompdf->render();
  40. // add the header
  41. $canvas = $dompdf->get_canvas();
  42. // Add font for page number
  43. $font = $dompdf->getFontMetrics()->get_font("Reglo", "normal");
  44. // Add page number
  45. $canvas->page_text(145, 390, "{PAGE_NUM} ",$font, 7, array(0,0,0));
  46. // Output the generated PDF to Browser
  47. //Change the name of the pdf by changing "Perec"
  48. $dompdf->stream("Perec", array("Attachment" => 0));
  49. ?>