Commit bc22d388 authored by jcnventura's avatar jcnventura

Move drush PDF download module-specific handling to each module.

parent 4e27010d
......@@ -11,65 +11,14 @@ PDF TOOL
--------
The print_pdf module requires the use of an external PDF generation tool.
The currently supported tools are dompdf, TCPDF and wkhtmltopdf. Please
note that any errors/bugs in those tools need to be reported and fixed by
their maintainers. DO NOT report bugs in those tools in the print module's
issue queue at Drupal.org.
Please note that any errors/bugs in those tools need to be reported and
fixed by their maintainers. DO NOT report bugs in those tools in the print
module's issue queue at drupal.org.
supported paths:
* print module lib directory (usually sites/all/modules/print/lib)
* libraries directory (sites/all/libraries)
dompdf support:
The dompdf tool produces results that are more faithful to the HTML
printer-friendly page. Good support of CSS 2.1 and partially CSS3.
1. Download dompdf from http://code.google.com/p/dompdf/downloads/list
2. Extract the contents of the downloaded package into one of the
supported paths.
3. Check if dompdf_config.inc.php fits your installation. In 99% of cases,
no changes are necessary, so just try to use it and only edit anything if
the PDF generation fails.
4. Grant write access to the lib/fonts directory to your webserver user.
5. If you're using dompdf-0.5.1, delete the dompdf.php file as it contains
a security vulnerability
6. If you're using dompdf-0.6 or later, you can try to enable the Unicode
support, but you'll need to add some Unicode fonts. See
http://groups.google.com/group/dompdf/browse_thread/thread/9f7bc0162b04d5cf
for further info on this.
7. Check http://code.google.com/p/dompdf/ for further information.
TCPDF support:
TCPDF's support for CSS is considerably worse than the other tools.
Unicode is supported (use of Unicode fonts result in HUGE files). Page
header and footer are supported. This module requires TCPDF >= 5.9.012.
1. Download TCPDF from http://sourceforge.net/projects/tcpdf/
2. Extract the contents of the downloaded package into one of the
supported paths. There is no need to modify the config/tcpdf_config.php
file, as the module self-configures TCPDF.
3. Grant write access to the cache and images directories to your
webserver user.
4. Check http://tcpdf.sourceforge.net/ for further information.
wkhtmltopdf support:
wkhtmltopdf is a webkit-based tool that actually is a browser in order to
generate the PDF. Resource hungry: expect to need some 30Mb+ of RAM and
some seconds of CPU power. The static binaries may need additional
libraries in your site, which may present problems in shared hosting
environments. The best, if you can run it.
1. Download wkhtmltopdf from
http://code.google.com/p/wkhtmltopdf/downloads/list. You can choose to
download the source and compile it or simply download the static binary,
which doesn't require you to compile anything. Note that the compiled
version may require a running X server (static uses patched libs that can
work without one).
2. Place the wkhtmltopdf executable into one of the supported paths.
(usually sites/all/modules/print/lib). You can also place a symbolic link
to the executable.
3. Check http://code.google.com/p/wkhtmltopdf/ for further information.
UPDATE
------
......
dompdf support:
The dompdf tool produces results that are more faithful to the HTML
printer-friendly page. Good support of CSS 2.1 and partially CSS3.
1. Download dompdf from http://code.google.com/p/dompdf/downloads/list
2. Extract the contents of the downloaded package into one of the
supported paths.
3. Check if dompdf_config.inc.php fits your installation. In 99% of cases,
no changes are necessary, so just try to use it and only edit anything if
the PDF generation fails.
4. Grant write access to the lib/fonts directory to your webserver user.
5. If you're using dompdf-0.5.1, delete the dompdf.php file as it contains
a security vulnerability
6. If you're using dompdf-0.6 or later, you can try to enable the Unicode
support, but you'll need to add some Unicode fonts. See
http://groups.google.com/group/dompdf/browse_thread/thread/9f7bc0162b04d5cf
for further info on this.
7. Check http://code.google.com/p/dompdf/ for further information.
<?php
/**
* @file
* drush integration for print_pdf_dompdf module PDF libraries download.
*/
/**
* The PDF project download URL
*/
// URI to the the latest dompdf version.. Hardcoded version unfortunately
define('DOMPDF_DOWNLOAD_URI', 'http://dompdf.googlecode.com/files/dompdf_0-6-0_beta3.tar.gz');
/**
* Implements hook_drush_command().
*/
function print_pdf_dompdf_drush_pdf_libs_alter(&$pdf_libs) {
$pdf_libs['dompdf'] = array(
'callback' => '_print_pdf_dompdf_drush_download_url',
);
}
/**
* Discover the correct URL of the package to download.
*
* @return string
* URL of the file to download, FALSE if not known
*/
function _print_pdf_dompdf_drush_download_url() {
return DOMPDF_DOWNLOAD_URI;
}
<?php
/**
* @file
* drush integration for print_pdf_mpdf module PDF libraries download.
*/
/**
* The PDF project download URL
*/
// URI to the the latest mpdf version.. Hardcoded version unfortunately
define('MPDF_DOWNLOAD_URI', 'http://mpdf1.com/repos/MPDF54.zip');
/**
* Implements hook_drush_command().
*/
function print_pdf_mpdf_drush_pdf_libs_alter(&$pdf_libs) {
$pdf_libs['mpdf'] = array(
'callback' => '_print_pdf_mpdf_drush_download_url',
);
}
/**
* Discover the correct URL of the package to download.
*
* @return string
* URL of the file to download, FALSE if not known
*/
function _print_pdf_mpdf_drush_download_url() {
return MPDF_DOWNLOAD_URI;
}
TCPDF support:
TCPDF's support for CSS is considerably worse than the other tools.
Unicode is supported (use of Unicode fonts result in HUGE files). Page
header and footer are supported. This module requires TCPDF >= 5.9.012.
1. Download TCPDF from http://sourceforge.net/projects/tcpdf/
2. Extract the contents of the downloaded package into one of the
supported paths. There is no need to modify the config/tcpdf_config.php
file, as the module self-configures TCPDF.
3. Grant write access to the cache and images directories to your
webserver user.
4. Check http://tcpdf.sourceforge.net/ for further information.
<?php
/**
* @file
* drush integration for print_pdf_tcpdf module PDF libraries download.
*/
/**
* The PDF project download URL
*/
// TCPDF is in sourceforge, and nicely provides a link to the latest version
define('TCPDF_DOWNLOAD_URI', 'http://sourceforge.net/projects/tcpdf/files/latest');
/**
* Implements hook_drush_command().
*/
function print_pdf_tcpdf_drush_pdf_libs_alter(&$pdf_libs) {
$pdf_libs['tcpdf'] = array(
'callback' => '_print_pdf_tcpdf_drush_download_url',
);
}
/**
* Discover the correct URL of the package to download.
*
* @return string
* URL of the file to download, FALSE if not known
*/
function _print_pdf_tcpdf_drush_download_url() {
return TCPDF_DOWNLOAD_URI;
}
wkhtmltopdf support:
wkhtmltopdf is a webkit-based tool that actually is a browser in order to
generate the PDF. Resource hungry: expect to need some 30Mb+ of RAM and
some seconds of CPU power. The static binaries may need additional
libraries in your site, which may present problems in shared hosting
environments. The best, if you can run it.
1. Download wkhtmltopdf from
http://code.google.com/p/wkhtmltopdf/downloads/list. You can choose to
download the source and compile it or simply download the static binary,
which doesn't require you to compile anything. Note that the compiled
version may require a running X server (static uses patched libs that can
work without one).
2. Place the wkhtmltopdf executable into one of the supported paths.
(usually sites/all/modules/print/lib). You can also place a symbolic link
to the executable.
3. Check http://code.google.com/p/wkhtmltopdf/ for further information.
<?php
/**
* @file
* drush integration for print_pdf_wkhtmltopdf module PDF libraries download.
*/
/**
* The PDF project download URL
*/
// wkhtmltopdf is a binary, requiring a different download for each platform
define('WKHTMLTOPDF_AMD64_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2');
define('WKHTMLTOPDF_I386_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2');
define('WKHTMLTOPDF_WIN_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltox-0.11.0_rc1-installer.exe');
define('WKHTMLTOPDF_OSX_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-OSX-0.10.0_rc2-static.tar.bz2');
/**
* Implements hook_drush_command().
*/
function print_pdf_wkhtmltopdf_drush_pdf_libs_alter(&$pdf_libs) {
$pdf_libs['wkhtmltopdf'] = array(
'callback' => '_print_pdf_wkhtmltopdf_drush_download_url',
);
}
/**
* Discover the correct URL of the package to download.
*
* @return string
* URL of the file to download, FALSE if not known
*/
function _print_pdf_wkhtmltopdf_drush_download_url() {
$ret = FALSE;
switch (drupal_substr(php_uname('s'), 0, 3)) {
case 'Lin':
$ret = (php_uname('m') == 'x86_64') ? WKHTMLTOPDF_AMD64_DOWNLOAD_URI : WKHTMLTOPDF_I386_DOWNLOAD_URI;
break;
case 'Win':
$ret = WKHTMLTOPDF_WIN_DOWNLOAD_URI;
break;
case 'Dar':
$ret = WKHTMLTOPDF_OSX_DOWNLOAD_URI;
break;
default:
drush_log(dt('wkhtmltopdf is not supported in this system, please choose another library.'), 'error');
break;
}
return $ret;
}
......@@ -9,28 +9,19 @@
* The PDF project download URL
*/
// TCPDF is in sourceforge, and nicely provides a link to the latest version
define('TCPDF_DOWNLOAD_URI', 'http://sourceforge.net/projects/tcpdf/files/latest');
// URI to the the latest dompdf version.. Hardcoded version unfortunately
define('DOMPDF_DOWNLOAD_URI', 'http://dompdf.googlecode.com/files/dompdf_0-6-0_beta3.tar.gz');
// wkhtmltopdf is a binary, requiring a different download for each platform
define('WKHTMLTOPDF_AMD64_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2');
define('WKHTMLTOPDF_I386_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2');
define('WKHTMLTOPDF_WIN_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltox-0.11.0_rc1-installer.exe');
define('WKHTMLTOPDF_OSX_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-OSX-0.10.0_rc2-static.tar.bz2');
/**
* Implements hook_drush_command().
*/
function print_pdf_drush_command() {
$items = array();
$pdf_libs = array();
drush_command_invoke_all_ref('drush_pdf_libs_alter', $pdf_libs);
$items['print-pdf-download'] = array(
'description' => 'Download and extract a PDF library.',
'arguments' => array(
'library' => dt('The PDF library to download. Either tcpdf, dompdf or wkhtmltopdf.'),
'library' => dt('The PDF library to download. Available choices: !libs.', array('!libs' => implode(', ', array_keys($pdf_libs)))),
),
'options' => array(
'path' => dt('A path to the download folder. If omitted Drush will use the default location (@path).', array('@path' => 'sites/all/libraries')),
......@@ -49,8 +40,13 @@ function print_pdf_drush_command() {
* library to download
*/
function drush_print_pdf_download($library) {
if (isset($library)) {
$download_url = _drush_print_pdf_download_url($library);
$pdf_libs = array();
drush_command_invoke_all_ref('drush_pdf_libs_alter', $pdf_libs);
if (isset($library) && isset($pdf_libs[drupal_strtolower($library)])) {
$func = $pdf_libs[drupal_strtolower($library)]['callback'];
$download_url = $func();
if ($download_url) {
$path = drush_get_option('path');
if (empty($path)) {
......@@ -74,9 +70,9 @@ function drush_print_pdf_download($library) {
}
// Download the archive
$filename = _drush_print_pdf_download_file($download_url);
$filename = _print_pdf_drush_download_file($download_url);
if ($filename) {
$extract_ret = _drush_print_pdf_download_extract($filename);
$extract_ret = _print_pdf_drush_download_extract($filename);
if ($extract_ret) {
// Remove the archive
drush_op('unlink', $filename);
......@@ -96,51 +92,57 @@ function drush_print_pdf_download($library) {
}
}
else {
drush_log(dt('Please specify a PDF library. Currently supported libraries are dompdf, tcpdf and wkhtmltopdf.'), 'error');
drush_log(dt('Please specify a PDF library. Available choices: !libs.', array('!libs' => implode(', ', array_keys($pdf_libs)))), 'error');
}
}
/**
* Discover the correct URL of the package to download.
* Download a file using wget or curl
*
* @param string $library
* library to download
* Adapted from a function in drush/includes/drush.inc to support 302 redirects.
*
* @param string $download_url
* The path to the file to download
*
* @return string
* URL of the file to download, FALSE if not known
* The filename that was downloaded, or NULL if the file could not be
* downloaded.
*/
function _drush_print_pdf_download_url($library) {
$ret = FALSE;
switch (drupal_strtolower($library)) {
case 'dompdf':
$ret = DOMPDF_DOWNLOAD_URI;
break;
case 'tcpdf':
$ret = TCPDF_DOWNLOAD_URI;
break;
case 'wkhtmltopdf':
switch (drupal_substr(php_uname('s'), 0, 3)) {
case 'Lin':
$ret = (php_uname('m') == 'x86_64') ? WKHTMLTOPDF_AMD64_DOWNLOAD_URI : WKHTMLTOPDF_I386_DOWNLOAD_URI;
break;
case 'Win':
$ret = WKHTMLTOPDF_WIN_DOWNLOAD_URI;
break;
case 'Dar':
$ret = WKHTMLTOPDF_OSX_DOWNLOAD_URI;
break;
default:
drush_log(dt('wkhtmltopdf is not supported in this system, please choose another library.'), 'error');
break;
function _print_pdf_drush_download_file($download_url) {
$wget_ret = drush_shell_exec("wget -nv --trust-server-names %s", $download_url);
if (!drush_get_context('DRUSH_SIMULATE')) {
if ($wget_ret) {
// Get the filename of the saved file from the output
$wget_out = explode('"', array_shift(drush_shell_exec_output()));
$filename = $wget_out[1];
}
else {
$tempnam = uniqid('print_pdf_drush_');
$curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
if ($curl_ret) {
// File was donwloaded with the tempname
// Find the effective name
$filename = explode('/', array_shift(drush_shell_exec_output()));
$filename = array_pop($filename);
// Rename file from tempname to effective name
if (!drush_op('rename', $tempnam, './' . $filename)) {
$filename = $tempnam;
}
}
else {
$filename = FALSE;
}
break;
default:
drush_log(dt('Unknown PDF library specified, please use one of the supported PDF libraries.'), 'error');
break;
}
}
else {
$filename = basename($download_url);
}
return $ret;
return $filename;
}
/**
......@@ -152,7 +154,7 @@ function _drush_print_pdf_download_url($library) {
* @return bool
* TRUE on success, FALSE on failure
*/
function _drush_print_pdf_download_extract($filename) {
function _print_pdf_drush_download_extract($filename) {
$arch_ret = FALSE;
if (drush_op('is_file', $filename)) {
......@@ -182,52 +184,3 @@ function _drush_print_pdf_download_extract($filename) {
return $arch_ret;
}
/**
* Download a file using wget or curl
*
* Adapted from a function in drush/includes/drush.inc to support 302 redirects.
*
* @param string $download_url
* The path to the file to download
*
* @return string
* The filename that was downloaded, or NULL if the file could not be
* downloaded.
*/
function _drush_print_pdf_download_file($download_url) {
$wget_ret = drush_shell_exec("wget -nv --trust-server-names %s", $download_url);
if (!drush_get_context('DRUSH_SIMULATE')) {
if ($wget_ret) {
// Get the filename of the saved file from the output
$wget_out = explode('"', array_shift(drush_shell_exec_output()));
$filename = $wget_out[1];
}
else {
$tempnam = uniqid('drush_print_pdf_');
$curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
if ($curl_ret) {
// File was donwloaded with the tempname
// Find the effective name
$filename = explode('/', array_shift(drush_shell_exec_output()));
$filename = array_pop($filename);
// Rename file from tempname to effective name
if (!drush_op('rename', $tempnam, './' . $filename)) {
$filename = $tempnam;
}
}
else {
$filename = FALSE;
}
}
}
else {
$filename = basename($download_url);
}
return $filename;
}
......@@ -38,14 +38,6 @@ function print_pdf_uninstall() {
variable_del('print_pdf_paper_size');
variable_del('print_pdf_pdf_tool');
variable_del('print_pdf_dompdf_unicode');
variable_del('print_pdf_font_family');
variable_del('print_pdf_font_size');
variable_del('print_pdf_font_subsetting');
variable_del('print_pdf_wkhtmltopdf_options');
variable_del('print_pdf_book_link');
variable_del('print_pdf_link_class');
variable_del('print_pdf_link_pos');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment