function init() { // Include the class file and create Html2ps instance App::import('vendor', 'Html2PsConfig', array('file' => 'html2ps' . DS . 'config.inc.php')); App::import('vendor', 'Html2Ps', array('file' => 'html2ps' . DS . 'pipeline.factory.class.php')); parse_config_file(APP . 'vendors' . DS . 'html2ps' . DS . 'html2ps.config'); global $g_config; $g_config = array('cssmedia' => 'screen', 'renderimages' => true, 'renderforms' => false, 'renderlinks' => true, 'mode' => 'html', 'debugbox' => false, 'draw_page_border' => false); $this->media = Media::predefined('A4'); $this->media->set_landscape(false); $this->media->set_margins(array('left' => 0, 'right' => 0, 'top' => 0, 'bottom' => 0)); $this->media->set_pixels(1024); global $g_px_scale; $g_px_scale = mm2pt($this->media->width() - $this->media->margins['left'] - $this->media->margins['right']) / $this->media->pixels; global $g_pt_scale; $g_pt_scale = $g_pt_scale * 1.43; $this->p = PipelineFactory::create_default_pipeline("", ""); switch ($this->output) { case 'download': $this->p->destination = new DestinationDownload($this->filename); break; case 'file': $this->p->destination = new DestinationFile($this->filename); break; default: $this->p->destination = new DestinationBrowser($this->filename); break; } }
function &preparePipeline(&$media) { $pipeline = PipelineFactory::create_default_pipeline("", ""); $pipeline->configure(array('scalepoints' => false)); $pipeline->data_filters[] = new DataFilterHTML2XHTML(); $pipeline->destination = new DestinationFile("test.pdf"); $pipeline->_prepare($media); return $pipeline; }
function testCSSParseAtRulesNestedContent() { $pipeline =& PipelineFactory::create_default_pipeline('utf-8', 'test.pdf'); $new_css_content =& parse_css_atpage_rules('body { background-color: green; } @page { @top-left { content: "TEXT"; } } #test { border: none; }', $pipeline); $this->assertEqual($new_css_content, 'body { background-color: green; } #test { border: none; }'); $content =& $pipeline->_page_at_rules[CSS_PAGE_SELECTOR_ALL][0]->margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT]->css->body->getPropertyValue(CSS_CONTENT); $this->assertNotNull($content); $this->assertEqual($content->render(new CSSCounterCollection()), "TEXT"); }
function runPipeline($html) { $pipeline = PipelineFactory::create_default_pipeline("", ""); $pipeline->configure(array('scalepoints' => false)); $pipeline->fetchers = array(new MyFetcherMemory($html, "")); $pipeline->data_filters[] = new DataFilterHTML2XHTML(); $pipeline->destination = new DestinationFile("test.pdf"); parse_config_file('../html2ps.config'); $media = Media::predefined("A5"); $pipeline->_prepare($media); return $pipeline->_layout_item("", $media, 0, $context, $positioned_filter); }
function testCSSParseMarginBoxesTopLeftSize() { parse_config_file('../html2ps.config'); $media =& Media::predefined('A4'); $media->set_margins(array('left' => 10, 'top' => 10, 'right' => 10, 'bottom' => 10)); $pipeline =& PipelineFactory::create_default_pipeline('utf-8', 'test.pdf'); $pipeline->_prepare($media); $pipeline->_cssState = array(new CSSState(CSS::get())); parse_css_atpage_rules('@page { @top-left { content: "TEXT"; } }', $pipeline); $boxes = $pipeline->reflow_margin_boxes(1, $media); $box =& $boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT]; $this->assertNotEqual($box->get_width(), 0); $expected_width = $pipeline->output_driver->stringwidth('TEXT', 'Times-Roman', 'iso-8859-1', 12); $this->assertTrue($box->get_width() >= $expected_width); $this->assertEqual($box->get_height(), mm2pt(10)); }
/** * Runs the HTML->PDF conversion with default settings * * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file, * use absolute links (like http://my.host/image.gif). * * @param $path_to_html String HTML code to be converted * @param $path_to_pdf String path to file to save generated PDF to. * @param $base_path String base path to use when resolving relative links in HTML code. */ function convert_to_pdf($html, $path_to_pdf, $base_path = '') { $pipeline = PipelineFactory::create_default_pipeline('', ''); // Override HTML source // @TODO: default http fetcher will return null on incorrect images // Bug submitted by 'imatronix' (tufat.com forum). $pipeline->fetchers[] = new MyFetcherMemory($html, $base_path); // Override destination to local file $pipeline->destination = new MyDestinationFile($path_to_pdf); $baseurl = ''; $media =& Media::predefined('A4'); $media->set_landscape(false); $media->set_margins(array('left' => 0, 'right' => 0, 'top' => 0, 'bottom' => 0)); $media->set_pixels(1024); 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->process_batch(array($baseurl), $media); }
/** * Runs the HTML->PDF conversion with default settings * * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file, * use absolute links (like http://my.host/image.gif). * * @param $path_to_html String path to source html file. * @param $path_to_pdf String path to file to save generated PDF to. */ function convert_to_pdf($path_to_html, $path_to_pdf) { $pipeline = PipelineFactory::create_default_pipeline("", ""); // Override HTML source $pipeline->fetchers[] = new MyFetcherLocalFile($path_to_html); $filter = new PreTreeFilterHeaderFooter("HEADER", "FOOTER"); $pipeline->pre_tree_filters[] = $filter; // Override destination to local file $pipeline->destination = new MyDestinationFile($path_to_pdf); $baseurl = ""; $media = Media::predefined("A4"); $media->set_landscape(false); $media->set_margins(array('left' => 0, 'right' => 0, 'top' => 10, 'bottom' => 10)); $media->set_pixels(1024); 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->process($baseurl, $media); }
{ var $_content; function MyFetcherLocalFile($file) { $this->_content = file_get_contents($file); } function get_data($dummy1) { return new FetchedDataURL($this->_content, array(), ""); } function get_base_url() { return ""; } } $pipeline = PipelineFactory::create_default_pipeline("", ""); // Override HTML source $pipeline->fetchers[] = new MyFetcherLocalFile($path_to_html); // Override destination to local file $pipeline->destination = new MyDestinationFile($path_to_pdf); $baseurl = ""; $media = Media::predefined("A4"); $media->set_landscape(false); $media->set_margins(array('left' => 0, 'right' => 0, 'top' => 0, 'bottom' => 0)); $media->set_pixels(1024); global $g_config; $g_config = array('cssmedia' => 'screen', 'renderimages' => true, 'renderlinks' => true, 'renderfields' => true, 'renderforms' => false, 'mode' => 'html', 'encoding' => '', 'debugbox' => false, 'pdfversion' => '1.4', 'draw_page_border' => false); global $g_px_scale; $g_px_scale = mm2pt($media->width() - $media->margins['left'] - $media->margins['right']) / $media->pixels; global $g_pt_scale; $g_pt_scale = $g_px_scale * 1.43;
function fn_html_to_pdf($html, $name) { if (!fn_init_pdf()) { fn_redirect(!empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : INDEX_SCRIPT); } $pipeline = PipelineFactory::create_default_pipeline('', ''); if (!is_array($html)) { $html = array($html); } $pipeline->fetchers = array(new PdfFetcherMemory($html, Registry::get('config.current_location') . '/'), new FetcherURL()); $pipeline->destination = new PdfDestinationDownload($name); $pipeline->data_filters = array(new DataFilterDoctype(), new DataFilterHTML2XHTML()); $media =& Media::predefined('A4'); $media->set_landscape(false); $media->set_margins(array('left' => 20, 'right' => 20, 'top' => 20, 'bottom' => 0)); $media->set_pixels(600); $_config = array('cssmedia' => 'print', 'scalepoints' => '1', 'renderimages' => true, 'renderlinks' => true, 'renderfields' => true, 'renderforms' => false, 'mode' => 'html', 'encoding' => 'utf8', 'debugbox' => false, 'pdfversion' => '1.4', 'draw_page_border' => false, 'smartpagebreak' => true); $pipeline->configure($_config); $pipeline->process_batch(array_keys($html), $media); }
function cw_pdf_generate($file, $template, $save_to_file = false, $landscape = false, $pages_limit = 0, $page_margins = array('10', '10', '10', '10'), $show_pages = true) { global $smarty, $var_dirs, $current_location; set_time_limit(2700); ini_set('memory_limit', '512M'); $smarty->assign('is_pdf', true); # kornev, only for A4 && 1024 p/wide $wcr = $hcr = 1024 / 210; $smarty->assign('wcr', $wcr); $smarty->assign('hcr', $hcr); if ($save_to_file && $file) { $html = $file; } else { $html = cw_display($template, $smarty, false); } parse_config_file(HTML2PS_DIR . 'html2ps.config'); $pipeline = PipelineFactory::create_default_pipeline('', ''); $pipeline->fetchers[] = new MyFetcherMemory($html, $current_location); if ($save_to_file) { $pipeline->destination = new MyDestinationFile($save_to_file); } else { $pipeline->destination = new DestinationDownload($file); } if ($show_pages) { $pipeline->pre_tree_filters[] = new PreTreeFilterHeaderFooter('', '<div>' . cw_get_langvar_by_name('lbl_page', null, false, true) . ' ##PAGE## / ##PAGES## </div>'); } $pipeline->pre_tree_filters[] = new PreTreeFilterHTML2PSFields(); $media =& Media::predefined('A4'); $media->set_landscape($landscape); $media->set_margins(array('left' => $page_margins[3], 'right' => $page_margins[1], 'top' => $page_margins[0], 'bottom' => $page_margins[2])); $media->set_pixels(1024); $g_config = array('cssmedia' => 'print', 'scalepoints' => '1', 'renderimages' => true, 'renderlinks' => false, 'renderfields' => false, 'renderforms' => false, 'mode' => 'html', 'encoding' => '', 'debugbox' => false, 'pdfversion' => '1.4', 'smartpagebreak' => true, 'draw_page_border' => false, 'html2xhtml' => false, 'method' => 'fpdf', 'pages_limit' => $pages_limit); $pipeline->configure($g_config); $pipeline->process_batch(array(''), $media); if (!$save_to_file) { exit(0); } }