false, 'margins' => array( 'top' => 0, 'bottom' => 0, 'left' => 0, 'right' => 0) 'mode' => 3 );
* PDFStart( $pdf_options );
*
* @param array $options PDF options (optional). Defaults see $default_options.
*
* @return array PDF options
*/
function PDFStart( $options = array() )
{
global $pdf_options;
$_REQUEST['_ROSARIO_PDF'] = true;
$default_options = array(
'css' => true, // Include CSS.
'margins' => array(), // Default margins.
'mode' => 2, // MODE_EMBEDDED.
'header_html' => '', // No HTML header.
'footer_html' => '', // No HTML footer.
'orientation' => '', // Portrait, can be set to 'landscape'.
);
$pdf_options = array_replace_recursive( $default_options, (array) $options );
// Do hook.
do_action( 'functions/PDF.php|pdf_start' );
// Start buffering.
ob_start();
return $pdf_options;
}
/**
* Get buffer and generate PDF
* Renders HTML if not wkhtmltopdf
*
* @since 3.4 Handle HTML header & footer.
* @since 4.3 CSS Add .wkhtmltopdf-header, .wkhtmltopdf-footer, .wkhtmltopdf-portrait & .wkhtmltopdf-landscape classes
* @since 7.5 Use phpwkhtmltopdf class instead of Wkhtmltopdf (more reliable & faster)
* @link https://github.com/mikehaertl/phpwkhtmltopdf
*
* @global string $wkhtmltopdfPath
* @global string $wkhtmltopdfAssetsPath
* @global string $RosarioPath
*
* @param array $handle from PDFStart(), PDF options.
*
* @return string Full path to file if Save mode, else outputs HTML if not wkhtmltopdf or Embed / Download PDF
*/
function PDFStop( $handle )
{
global $wkhtmltopdfPath,
$wkhtmltopdfAssetsPath,
$RosarioPath;
static $file_number = 1;
if ( ! $handle )
{
return '';
}
$handle['orientation'] = empty( $_SESSION['orientation'] ) ? $handle['orientation'] : $_SESSION['orientation'];
unset( $_SESSION['orientation'] );
// Get buffer.
$html_content = ob_get_clean();
$lang_2_chars = mb_substr( $_SESSION['locale'], 0, 2 );
// Right to left direction.
$RTL_languages = array( 'ar', 'he', 'dv', 'fa', 'ur' );
$dir_RTL = in_array( $lang_2_chars, $RTL_languages ) ? ' dir="RTL"' : '';
// Page width.
// @see wkhtmltopdf.css.
$orientation_class = 'wkhtmltopdf-portrait'; // 994px, originally 1024px.
if ( $handle['orientation'] === 'landscape' )
{
$orientation_class = 'wkhtmltopdf-landscape'; // 1405px, originally 1448px.
}
// Page title.
$page_title = str_replace( _( 'Print' ) . ' ', '', ProgramTitle() );
$_html = array();
// Convert to HTML page with CSS.
$_html['head'] = '
';
if ( $handle['css'] )
{
$_html['head'] .= '';
}
// Include Markdown to HTML.
// @since 6.0 JS MarkDown use marked instead of showdown (15KB smaller).
$_html['head'] .= '';
// Include wkhtmltopdf Warehouse JS functions.
$_html['head'] .= '';
// FJ bugfix wkhtmltopdf screen resolution on linux
// see: https://code.google.com/p/wkhtmltopdf/issues/detail?id=118
$_html['head'] .= '' . $page_title . '
';
$_html['foot'] = '
';
$html = $_html['head'] . $html_content . $_html['foot'];
// Create PDF in the temporary files system directory.
$path = sys_get_temp_dir();
// File name.
$filename = utf8_decode( str_replace(
array( _( 'Print' ) . ' ', ' ' ),
array( '', '_' ),
ProgramTitle()
)) . ( $file_number++ );
if ( empty( $wkhtmltopdfPath ) )
{
// If no wkhtmltopdf, render in HTML.
if ( $handle['mode'] !== 3 ) // Display HTML.
{
echo $html;
return '';
}
// Save.
$base_url = sprintf(
'%s://%s%s/',
isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
dirname( $_SERVER['PHP_SELF'] )
);
// Set Absolute URLs to images, CSS...
$html = str_replace( '"assets/', '"' . $base_url . 'assets/', $html );
$html = str_replace( '"modules/', '"' . $base_url . 'modules/', $html );
file_put_contents( $path . DIRECTORY_SEPARATOR . $filename . '.html', $html );
$full_path = $path . DIRECTORY_SEPARATOR . $filename . '.html';
return $full_path;
}
// Load phpwkhtmltopdf class.
require_once 'classes/phpwkhtmltopdf/php-shellcommand/Command.php';
require_once 'classes/phpwkhtmltopdf/php-tmpfile/File.php';
require_once 'classes/phpwkhtmltopdf/Command.php';
require_once 'classes/phpwkhtmltopdf/Pdf.php';
// You can override the Path definition in the config.inc.php file.
if ( ! isset( $wkhtmltopdfAssetsPath ) )
{
// Way wkhtmltopdf accesses the assets/ directory, empty string means no translation.
$wkhtmltopdfAssetsPath = $RosarioPath . 'assets/';
}
if ( ! empty( $wkhtmltopdfAssetsPath ) )
{
// Fix wkhtmltopdf error on Windows: prepend file:///.
$html = str_replace( '"assets/', '"file:///' . $wkhtmltopdfAssetsPath, $html );
$_html['head'] = str_replace( '"assets/', '"file:///' . $wkhtmltopdfAssetsPath, $_html['head'] );
}
// Fix wkhtmltopdf error on Windows: prepend file:///.
$html = str_replace( '"modules/', '"file:///' . $RosarioPath . 'modules/', $html );
// Set wkhtmltopdf options.
$pdf_options = array(
'title' => $page_title,
// Fix System error "blocked access to local file" with wkhtmltopdf 0.12.6.
'enable-local-file-access',
);
if ( Preferences( 'PAGE_SIZE' ) != 'A4' )
{
$pdf_options['page-size'] = Preferences( 'PAGE_SIZE' );
}
if ( ! empty( $handle['orientation'] )
&& $handle['orientation'] === 'landscape' )
{
$pdf_options['orientation'] = 'Landscape';
}
if ( ! empty( $handle['margins'] )
&& is_array( $handle['margins'] ) )
{
foreach ( $handle['margins'] as $position => $margin )
{
if ( is_null( $margin ) )
{
continue;
}
$pdf_options['margin-' . $position] = $margin;
}
}
if ( $handle['header_html'] )
{
$header_html = $handle['header_html'];
if ( mb_stripos( $header_html, 'binary = $wkhtmltopdfPath;
$pdf->addPage( $html );
if ( $handle['mode'] === 3 ) // Save.
{
$full_path = $path . DIRECTORY_SEPARATOR . $filename . '.pdf';
// Save the PDF.
if ( ! $pdf->saveAs( $full_path ) )
{
echo ErrorMessage( $pdf->getError() );
}
return $full_path;
}
// Send to client as file download.
if ( ! $pdf->send( $filename . '.pdf', (bool) $handle['mode'] ) ) // Embed or Download.
{
echo ErrorMessage( $pdf->getError() );
}
return '';
}