1<?php
2
3/**
4 * Thanks for JensE for providing the code of fetcher class
5 */
6
7require_once(dirname(__FILE__).'/../config.inc.php');
8require_once(HTML2PS_DIR.'pipeline.factory.class.php');
9
10error_reporting(E_ALL);
11ini_set("display_errors","1");
12@set_time_limit(10000);
13parse_config_file(HTML2PS_DIR.'html2ps.config');
14
15/**
16 * Handles the saving generated PDF to user-defined output file on server
17 */
18class MyDestinationFile extends Destination {
19  /**
20   * @var String result file name / path
21   * @access private
22   */
23  var $_dest_filename;
24
25  function MyDestinationFile($dest_filename) {
26    $this->_dest_filename = $dest_filename;
27  }
28
29  function process($tmp_filename, $content_type) {
30    copy($tmp_filename, $this->_dest_filename);
31  }
32}
33
34class MyFetcherMemory extends Fetcher {
35  var $base_path;
36  var $content;
37
38  function MyFetcherMemory($content, $base_path) {
39    $this->content   = $content;
40    $this->base_path = $base_path;
41  }
42
43  function get_data($url) {
44    if (!$url) {
45      return new FetchedDataURL($this->content, array(), "");
46    } else {
47      // remove the "file:///" protocol
48      if (substr($url,0,8)=='file:///') {
49        $url=substr($url,8);
50        // remove the additional '/' that is currently inserted by utils_url.php
51        if (PHP_OS == "WINNT") $url=substr($url,1);
52      }
53      return new FetchedDataURL(@file_get_contents($url), array(), "");
54    }
55  }
56
57  function get_base_url() {
58    return 'file:///'.$this->base_path.'/dummy.html';
59  }
60}
61
62/**
63 * Runs the HTML->PDF conversion with default settings
64 *
65 * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file,
66 * use absolute links (like http://my.host/image.gif).
67 *
68 * @param $path_to_html String HTML code to be converted
69 * @param $path_to_pdf  String path to file to save generated PDF to.
70 * @param $base_path    String base path to use when resolving relative links in HTML code.
71 */
72function convert_to_pdf($html, $path_to_pdf, $base_path='') {
73  $pipeline = PipelineFactory::create_default_pipeline('', // Attempt to auto-detect encoding
74                                                       '');
75
76  // Override HTML source
77  // @TODO: default http fetcher will return null on incorrect images
78  // Bug submitted by 'imatronix' (tufat.com forum).
79  $pipeline->fetchers[] = new MyFetcherMemory($html, $base_path);
80
81  // Override destination to local file
82  $pipeline->destination = new MyDestinationFile($path_to_pdf);
83
84  $baseurl = '';
85  $media =& Media::predefined('A4');
86  $media->set_landscape(false);
87  $media->set_margins(array('left'   => 0,
88                            'right'  => 0,
89                            'top'    => 0,
90                            'bottom' => 0));
91  $media->set_pixels(1024);
92
93  global $g_config;
94  $g_config = array(
95                    'cssmedia'     => 'screen',
96                    'scalepoints'  => '1',
97                    'renderimages' => true,
98                    'renderlinks'  => true,
99                    'renderfields' => true,
100                    'renderforms'  => false,
101                    'mode'         => 'html',
102                    'encoding'     => '',
103                    'debugbox'     => false,
104                    'pdfversion'    => '1.4',
105                    'draw_page_border' => false
106                    );
107
108  $pipeline->configure($g_config);
109  $pipeline->process_batch(array($baseurl), $media);
110}
111
112convert_to_pdf(file_get_contents('../temp/long.html'), '../out/test.pdf');
113
114?>