convert html to pdf with html2ps


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*******************************************************************************
  2.  *
  3.  */
  4. function convert_to_pdf($url, $path_to_pdf) {
  5. require_once(dirname(__FILE__).'/html2ps/config.inc.php');
  6. require_once(HTML2PS_DIR.'pipeline.factory.class.php');
  7. echo WRITER_TEMPDIR;
  8. //error_reporting(E_ALL);
  9. //ini_set("display_errors","1");
  10. @set_time_limit(10000);
  11. parse_config_file(HTML2PS_DIR.'html2ps.config');
  12.  
  13. /**
  14.   * Handles the saving generated PDF to user-defined output file on server
  15.   */
  16. class MyDestinationFile extends Destination {
  17. /**
  18.   * @var String result file name / path
  19.   * @access private
  20.   */
  21. var $_dest_filename;
  22.  
  23. function MyDestinationFile($dest_filename) {
  24. $this->_dest_filename = $dest_filename;
  25. }
  26.  
  27. function process($tmp_filename, $content_type) {
  28. copy($tmp_filename, $this->_dest_filename);
  29. }
  30. }
  31.  
  32.  
  33. $media = Media::predefined("A4");
  34. $media->set_landscape(false);
  35. $media->set_margins(array('left' => 5,
  36. 'right' => 5,
  37. 'top' => 10,
  38. 'bottom' => 10));
  39. $media->set_pixels(800);
  40.  
  41. $pipeline = PipelineFactory::create_default_pipeline("", // Attempt to auto-detect encoding
  42. "");
  43. // Override HTML source
  44. $pipeline->fetchers[] = new FetcherURL;
  45. $pipeline->data_filters[] = new DataFilterHTML2XHTML;
  46. $pipeline->parser = new ParserXHTML;
  47. $pipeline->layout_engine = new LayoutEngineDefault;
  48.  
  49. $pipeline->output_driver = new OutputDriverFPDF($media);
  50. //$filter = new PreTreeFilterHeaderFooter("HEADER", "FOOTER");
  51. //$pipeline->pre_tree_filters[] = $filter;
  52.  
  53. // Override destination to local file
  54. $pipeline->destination = new MyDestinationFile($path_to_pdf);
  55.  
  56. global $g_config;
  57. $g_config = array(
  58. 'cssmedia' => 'screen',
  59. 'scalepoints' => '1',
  60. 'renderimages' => true,
  61. 'renderlinks' => true,
  62. 'renderfields' => true,
  63. 'renderforms' => false,
  64. 'mode' => 'html',
  65. 'encoding' => '',
  66. 'debugbox' => false,
  67. 'pdfversion' => '1.4',
  68. 'draw_page_border' => false
  69. );
  70. $pipeline->configure($g_config);
  71. //$pipeline->add_feature('toc', array('location' => 'before'));
  72. $pipeline->process($url, $media);
  73. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.