Return to Snippet

Revision: 50953
at September 9, 2011 23:00 by kspal


Initial Code
/*******************************************************************************
 *
 */
function convert_to_pdf($url, $path_to_pdf) {
  require_once(dirname(__FILE__).'/html2ps/config.inc.php');
  require_once(HTML2PS_DIR.'pipeline.factory.class.php');
  echo WRITER_TEMPDIR;
  //error_reporting(E_ALL);
  //ini_set("display_errors","1");
  @set_time_limit(10000);
  parse_config_file(HTML2PS_DIR.'html2ps.config');

  /**
   * Handles the saving generated PDF to user-defined output file on server
   */
  class MyDestinationFile extends Destination {
    /**
     * @var String result file name / path
     * @access private
     */
    var $_dest_filename;

    function MyDestinationFile($dest_filename) {
      $this->_dest_filename = $dest_filename;
    }

    function process($tmp_filename, $content_type) {
      copy($tmp_filename, $this->_dest_filename);
    }
  }


  $media = Media::predefined("A4");
  $media->set_landscape(false);
  $media->set_margins(array('left'   => 5,
                            'right'  => 5,
                            'top'    => 10,
                            'bottom' => 10));
  $media->set_pixels(800);

  $pipeline = PipelineFactory::create_default_pipeline("", // Attempt to auto-detect encoding
                                                       "");
  // Override HTML source
  $pipeline->fetchers[]     = new FetcherURL;
  $pipeline->data_filters[] = new DataFilterHTML2XHTML;
  $pipeline->parser         = new ParserXHTML;
  $pipeline->layout_engine  = new LayoutEngineDefault;

    $pipeline->output_driver  = new OutputDriverFPDF($media);
  //$filter = new PreTreeFilterHeaderFooter("HEADER", "FOOTER");
  //$pipeline->pre_tree_filters[] = $filter;

  // Override destination to local file
  $pipeline->destination = new MyDestinationFile($path_to_pdf);

  global $g_config;
  $g_config = array(
                    'cssmedia'     => 'screen',
                    'scalepoints'  => '1',
                    'renderimages' => true,
                    'renderlinks'  => true,
                    'renderfields' => true,
                    'renderforms'  => false,
                    'mode'         => 'html',
                    'encoding'     => '',
                    'debugbox'     => false,
                    'pdfversion'    => '1.4',
                    'draw_page_border' => false
                    );
  $pipeline->configure($g_config);
  //$pipeline->add_feature('toc', array('location' => 'before'));
  $pipeline->process($url, $media);
}

Initial URL

                                

Initial Description

                                

Initial Title
convert html to pdf with html2ps

Initial Tags
html, convert

Initial Language
PHP