| | | | |
| | |
| Erfahrener Benutzer Registriert seit: 01.06.2004
Beiträge: 136
![]() | Hallo! Ich möchte aus HTML Dateien auf Knopfdruck ein PDF erstellen lassen. Dafür benutze ich html2pdf von www.rustyparts.com. Ich teste ein beigelegtes Beipspiel und es erscheint immer die Fehlermedlung: Error: html2ps [/usr/bin/html2ps] not executable Kennt sich jemand damit aus! Kann mir sagen, was falsch ist. Der Code von der Klasse: Code: <?php
/** $Id: HTML_ToPDF.php 2063 2006-01-10 17:09:37Z jrust $ */
// {{{ license
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/3_0.txt |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jason Rust <jrust@rustyparts.com> |
// +----------------------------------------------------------------------+
// }}}
// {{{ includes
require_once 'PEAR.php';
// }}}
// {{{ HTML_ToPDF class
/**
* A class to convert a local html file to a pdf file on the fly.
*
* Will take a local or remote html file and convert it to a PDF file. Note
* that you can add encryption or permissions to the PDF file by using the helper
* PDFEncryptor class that comes with this package. See the README and
* examples for more information.
*
* @author Jason Rust <jrust@rustyparts.com>
* @version 3.4
* @package HTML_ToPDF
* @copyright The PHP License
*/
// }}}
class HTML_ToPDF {
// {{{ properties
/**
* The full path to the file we are parsing
* @var string
*/
var $htmlFile = '';
/**
* The full path to the output file
* @var string
*/
var $pdfFile = '';
/**
* The temporary directory to save intermediate files
* @var string
*/
var $tmpDir = '/tmp';
/**
* Whether or not we are in debug mode
* @var bool
*/
var $debug = false;
/**
* Whether we output html errors.
* @var bool
*/
var $htmlErrors = false;
/**
* The default domain for relative and absolute paths in
* images, css, etc.
* @var string
*/
var $defaultDomain = '';
/**
* The default path for relative paths in images, css etc.
* @var string
*/
var $defaultPath = '/';
/**
* The path to the html2ps executable
* @var string
*/
var $html2psPath = '/usr/bin/html2ps';
/**
* The path to the ps2pdf executable
* @var string
*/
var $ps2pdfPath = '/usr/bin/ps2pdf';
/**
* The path to your get URL program, including options to get headers
* @var string
*/
var $getUrlPath = '/usr/bin/curl -i';
/**
* Whether or not to try and parse the CSS in the html file and use it in
* creating the pdf
* @var bool
*/
var $useCSS = true;
/**
* Other styles to use when parsing the page
* @var string
*/
var $additionalCSS = '';
/**
* Show the page in color?
* @var bool
*/
var $pageInColor = true;
/**
* Show the images be in grayscale?
* @var bool
*/
var $grayScale = false;
/**
* Scale factore for the page
* @var int
*/
var $scaleFactor = 1;
/**
* Whether to underline links or not
* @var bool
*/
var $underlineLinks = null;
/**
* The header information
* @var array
*/
var $headers = array('left' => '$T', 'right' => '$[author]');
/**
* The footer information
* @var array
*/
var $footers = array('center' => '- $N -');
/**
* Default html2ps configuration that we use (is parsed before being used, though)
* @var string
*/
var $html2psrc = '
option {
titlepage: 0; /* do not generate a title page */
toc: 0; /* no table of contents */
colour: %pageInColor%; /* create the page in color */
underline: %underlineLinks%; /* underline links */
grayscale: %grayScale%; /* Make images grayscale? */
scaledoc: %scaleFactor%; /* Scale the document */
}
package {
geturl: %getUrlPath%; /* path to the geturl */
}
showurl: 0; /* do not show the url next to links */';
/**
* Whether HTML_ToPDF should replace all relative image paths in the
* input HTML document with the default domain or not. Switch this to
* false if you want to convert a HTML file which is located locally in the
* file system and is not reachable via HTTP but all the images used
* in the HTML file are located correctly according to their relative
* paths.
* @var bool
*/
var $makeAbsoluteImageUrls = true;
/**
* Include path for ps2pdf (-I option), for example to specify where
* to search for font files etc.
* @var string
*/
var $ps2pdfIncludePath = '';
/**
* We use this to store the html file to a string for manipulation
* @var string
*/
var $_htmlString = '';
// }}}
// {{{ constructor
/**
* Initializes the class
*
* @param string $in_htmlFile The full path to the html file to convert
* @param string $in_domain The default domain name for images that have a absolute or relative path
* @param string $in_pdfFile (optional) The full path to the pdf file to output.
* If not given then we create a temporary name.
*
* @access public
* @return void
*/
function HTML_ToPdf($in_htmlFile, $in_domain, $in_pdfFile = null)
{
$this->htmlFile = $in_htmlFile;
$this->defaultDomain = $in_domain;
// We'll set it to a temporary name later, if needed, so that tmpDir can be set.
$this->pdfFile = $in_pdfFile;
$this->htmlErrors = (php_sapi_name() != 'cli' && !(substr(php_sapi_name(),0,3)=='cgi' &&
!isset($_SERVER['GATEWAY_INTERFACE'])));
}
// }}}
// {{{ addHtml2PsSettings()
/**
* Adds on more html2ps settings to the end of the default set of settings
*
* @param string $in_settings The additional settings
*
* @access public
* @return void
*/
function addHtml2PsSettings($in_settings) {
$this->html2psrc .= "\n" . $in_settings;
}
// }}}
// {{{ setDefaultPath()
/**
* Sets the default path for relative paths in images, css etc.
*
* @param string $in_path A web-path, default is /
*
* @access public
* @return void
*/
function setDefaultPath($in_path)
{
// Default paths should always have a trailing slash...
if ($in_path{strlen($in_path) - 1} != '/') {
$in_path .= '/';
}
$this->defaultPath = $in_path;
}
// }}}
// {{{ setDebug()
/**
* Sets the debug variable
*
* @param bool $in_debug Turn debugging on or off?
*
* @access public
* @return void
*/
function setDebug($in_debug)
{
$this->debug = $in_debug;
}
// }}}
// {{{ setHeader()
/**
* Sets a header
*
* @param string $in_attribute One of the header attributes that html2ps accepts. Most
* common are left, center, right, font-family, font-size, color.
* @param string $in_value The attribute value. Special values that can be set are $T
* (document title), $N (page number), $D (current date/time), $U (current
* url or filename), $[meta-name] (A meta-tag, such as $[author] to get
* author meta tag)
*
* @access public
* @return void
*/
function setHeader($in_attribute, $in_value)
{
$this->headers[$in_attribute] = $in_value;
}
// }}}
// {{{ setFooter()
/**
* Sets a footer
*
* @param string $in_attribute One of the header attributes that html2ps accepts. Most
* common are left, center, right, font-family, font-size, color.
* @param string $in_value The attribute value. Special values that can be set are $T
* (document title), $N (page number), $D (current date/time), $U (current
* url or filename), $[meta-name] (A meta-tag, such as $[author] to get
* author meta tag)
*
* @access public
* @return void
*/
function setFooter($in_attribute, $in_value)
{
$this->footers[$in_attribute] = $in_value;
}
// }}}
// {{{ setTmpDir()
/**
* Set the temporary directory path
*
* @param string $in_path The full path to the tmp dir
*
* @access public
* @return void
*/
function setTmpDir($in_path) {
$this->tmpDir = $in_path;
}
// }}}
// {{{ setUseColor()
/**
* Set whether to use color or not when creating the page
*
* @param bool $in_useColor Use color?
*
* @access public
* @return void
*/
function setUseColor($in_useColor) {
$this->pageInColor = $in_useColor;
}
// }}}
// {{{ setUseCSS()
/**
* Set whether to try and use the CSS in the html page when creating
* the pdf file
*
* @param bool $in_useCSS Use CSS found in html file?
*
* @access public
* @return void
*/
function setUseCSS($in_useCSS) {
$this->useCSS = $in_useCSS;
}
// }}}
// {{{ setAdditionalCSS()
/**
* Set additional CSS to use when parsing the html file
*
* @param string $in_css The additional css
*
* @access public
* @return void
*/
function setAdditionalCSS($in_css) {
$this->additionalCSS = $in_css;
}
// }}}
// {{{ setGetUrlPath()
/**
* Sets the get url which is used for retrieving images from the html file
* needs to be the full path to the file with options to retrive the headers
* as well.
*
* @param string $in_getUrl The get url program
*
* @access public
* @return void
*/
function setGetUrl($in_getUrl) {
$this->getUrlPath = $in_getUrl;
}
// }}}
// {{{ setGrayScale()
/**
* Sets the gray scale option for images
*
* @param bool $in_grayScale Images should be in grayscale?
*
* @access public
* @return void
*/
function setGrayScale($in_grayScale) {
$this->grayScale = $in_grayScale;
}
// }}}
// {{{ setUnderlineLinks()
/**
* Sets the option to underline links or not
*
* @param bool $in_underline Links should be underlined?
*
* @access public
* @return void
*/
function setUnderlineLinks($in_underline) {
$this->underlineLinks = $in_underline;
}
// }}}
// {{{ setScaleFactor()
/**
* Sets the scale factor for the page. Less than one makes it smaller,
* greater than one enlarges it.
*
* @param int $in_scale Scale factor
*
* @access public
* @return void
*/
function setScaleFactor($in_scale) {
$this->scaleFactor = $in_scale;
}
// }}}
// {{{ setHtml2Ps()
/**
* Sets the path to the html2ps program
*
* @param string $in_html2ps The html2ps program
*
* @access public
* @return void
*/
function setHtml2Ps($in_html2ps) {
$this->html2psPath = $in_html2ps;
}
// }}}
// {{{ setPs2Pdf()
/**
* Sets the path to the ps2pdf program
*
* @param string $in_ps2pdf The ps2pdf program
*
* @access public
* @return void
*/
function setPs2Pdf($in_ps2pdf) {
$this->ps2pdfPath = $in_ps2pdf;
}
// }}}
// {{{ setMakeAbsoluteImageUrls()
/**
* Sets the makeAbsoluteImageUrls variable
*
* @param bool $in_makeAbsoluteImageUrls Replace relative image
* URLs in the input HTML
* file with default domain?
*
* @access public
* @return void
*/
function setMakeAbsoluteImageUrls($in_makeAbsoluteImageUrls)
{
$this->makeAbsoluteImageUrls = $in_makeAbsoluteImageUrls;
}
// }}}
// {{{ setPs2pdfIncludePath()
/**
* Sets the ps2pdfIncludePath() variable
*
* @param string $in_ps2pdfIncludePath The include path for ps2pdf
*
* @access public
* @return void
*/
function setPs2pdfIncludePath($in_ps2pdfIncludePath)
{
$this->ps2pdfIncludePath = $in_ps2pdfIncludePath;
}
// }}}
// {{{ convert()
/**
* Convert the html file into a pdf file
*
* @access public
* @return string The path to the pdf file
*/
function convert()
{
// make sure html file exists
if (!file_exists($this->htmlFile) && !preg_match(':^(f|ht)tps?\://:i', $this->htmlFile)) {
return new HTML_ToPDFException("Error: The HTML file does not exist: $this->htmlFile");
}
// first make sure we can execute the programs
// html2ps is just a perl script on windows though
if (!OS_WINDOWS && !@is_executable($this->html2psPath)) {
return new HTML_ToPDFException("Error: html2ps [$this->html2psPath] not executable");
}
if (!@is_executable($this->ps2pdfPath)) {
return new HTML_ToPDFException("Error: ps2pdf [$this->ps2pdfPath] not executable");
}
// this can take a while with large files
set_time_limit(160);
// read the html file in so we can modify it
$this->_htmlString = @implode('', @file($this->htmlFile));
// grab extra CSS
$this->additionalCSS .= $this->_getCSSFromFile();
// modify the conf file
$this->_modifyConfFile();
$paperSize = $this->_getPaperSize();
$orientation = $this->_getOrientation();
if ($this->makeAbsoluteImageUrls) {
// prepend relative image paths with the default domain and path
$this->_htmlString = preg_replace(':Der Code von der Testseite im Unterordner "examples": Code: <?php
/** $Id: example1.php 923 2003-11-18 17:18:40Z jrust $ */
/**
* The simplest example. We convert an HTML file into a PDF file.
* We also add a few custom headers/footers to the PDF.
*/
?>
<html>
<head>
<title>Testing HTML_ToPDF</title>
</head>
<body>
Creating the PDF from local HTML file.... Note that we customize the headers and footers!
<?php
// Require the class
require_once dirname(__FILE__) . '/../HTML_ToPDF.php';
// Full path to the file to be converted
$htmlFile = dirname(__FILE__) . '/test.html';
// The default domain for images that use a relative path
// (you'll need to change the paths in the test.html page
// to an image on your server)
$defaultDomain = 'www.rustyparts.com';
// Full path to the PDF we are creating
$pdfFile = dirname(__FILE__) . '/timecard.pdf';
// Remove old one, just to make sure we are making it afresh
@unlink($pdfFile);
// Instnatiate the class with our variables
$pdf =& new HTML_ToPDF($htmlFile, $defaultDomain, $pdfFile);
// Set headers/footers
$pdf->setHeader('color', 'blue');
$pdf->setFooter('left', 'Generated by HTML_ToPDF');
$pdf->setFooter('right', '$D');
$result = $pdf->convert();
// Check if the result was an error
if (PEAR::isError($result)) {
die($result->getMessage());
}
else {
echo "PDF file created successfully: $result";
echo '
Click here to view the PDF file.';
}
?>
</body>
</html>
|
| |
| | |
| PHP Code Flüsterer Registriert seit: 21.08.2005 Beiträge: 4682 PHP-Kenntnisse: Fortgeschritten | |
| | |
| Erfahrener Benutzer Registriert seit: 21.05.2008
Beiträge: 9.937
![]() | 3 km Code sind hier eigentlich eher unwillkommen... Setz mal chmod-Rechte für diesen Ordner http://de.php.net/chmod Ist das die komplette Fehlermeldung? |
| |
| Themen-Optionen | |
| Thema bewerten | |
|
|
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| html2pdf einbinden ??? | NetLook | PHP Tipps 2006 | 0 | 24.04.2006 10:51 |
| html2pdf: externe css einbinden | webazubi | PHP-Fortgeschrittene | 8 | 21.03.2006 01:04 |
| html2pdf: externe CSS einbinden | webazubi | PHP Tipps 2006 | 0 | 28.02.2006 16:36 |
| Problem bei der PDF Generierung HTML2PDF von rustyparts.com | joni1980 | PHP-Fortgeschrittene | 2 | 23.09.2005 16:15 |
| problem mit html2pdf | Sclot | PHP Tipps 2005-2 | 4 | 21.06.2005 15:08 |

Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.