1<?php
2
3require_once(dirname(__FILE__).'/../config.inc.php');
4require_once(HTML2PS_DIR.'pipeline.factory.class.php');
5
6error_reporting(E_ALL);
7ini_set("display_errors","1");
8@set_time_limit(10000);
9parse_config_file(HTML2PS_DIR.'html2ps.config');
10
11/**
12 * Handles the saving generated PDF to user-defined output file on server
13 */
14class MyDestinationFile extends Destination {
15  /**
16   * @var String result file name / path
17   * @access private
18   */
19  var $_dest_filename;
20
21  function MyDestinationFile($dest_filename) {
22    $this->_dest_filename = $dest_filename;
23  }
24
25  function process($tmp_filename, $content_type) {
26    copy($tmp_filename, $this->_dest_filename);
27  }
28}
29
30class MyFetcherLocalFile extends Fetcher {
31  var $_content;
32
33  function MyFetcherLocalFile($file) {
34    $this->_content = file_get_contents($file);
35  }
36
37  function get_data($dummy1) {
38    return new FetchedDataURL($this->_content, array(), "");
39  }
40
41  function get_base_url() {
42    return "file:///C:/rac/html2ps/test/";
43  }
44}
45
46/**
47 * Runs the HTML->PDF conversion with default settings
48 *
49 * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file,
50 * use absolute links (like http://my.host/image.gif).
51 *
52 * @param $path_to_html String path to source html file.
53 * @param $path_to_pdf  String path to file to save generated PDF to.
54 */
55function convert_to_pdf($path_to_html, $path_to_pdf) {
56  $pipeline = PipelineFactory::create_default_pipeline("", // Attempt to auto-detect encoding
57                                                       "");
58  // Override HTML source
59  $pipeline->fetchers[] = new MyFetcherLocalFile($path_to_html);
60
61  $filter = new PreTreeFilterHeaderFooter("HEADER", "FOOTER");
62  $pipeline->pre_tree_filters[] = $filter;
63
64  // Override destination to local file
65  $pipeline->destination = new MyDestinationFile($path_to_pdf);
66
67  $baseurl = "";
68  $media = Media::predefined("A4");
69  $media->set_landscape(false);
70  $media->set_margins(array('left'   => 0,
71                            'right'  => 0,
72                            'top'    => 10,
73                            'bottom' => 10));
74  $media->set_pixels(1024);
75
76  global $g_config;
77  $g_config = array(
78                    'cssmedia'     => 'screen',
79                    'scalepoints'  => '1',
80                    'renderimages' => true,
81                    'renderlinks'  => true,
82                    'renderfields' => true,
83                    'renderforms'  => false,
84                    'mode'         => 'html',
85                    'encoding'     => '',
86                    'debugbox'     => false,
87                    'pdfversion'    => '1.4',
88                    'draw_page_border' => false
89                    );
90  $pipeline->configure($g_config);
91  $pipeline->add_feature('toc', array('location' => 'before'));
92  $pipeline->process($baseurl, $media);
93}
94
95convert_to_pdf("../temp/test.html", "../out/test.pdf");
96?>