Last active
December 6, 2017 09:46
-
-
Save vensires/9a2a56e8de571c4c8cea117e723393d4 to your computer and use it in GitHub Desktop.
Find if a library exists in a Drupal path recognisable by Libraries module without needing libraries_load() or an implementation of hook_libraries_info(). This code is looking for the MPDF library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function to check existence of mPDF library. | |
* | |
* @return bool | |
* TRUE if mPDF library path exists and FALSE if it isn't. | |
*/ | |
function module_mpdf_exists() { | |
// Search for mpdf tool first. | |
$pattern = '/^mpdf.php$/'; | |
// Use the libraries module to detect mPDF library in case of multisite | |
// installation. | |
$tools = array_keys(file_scan_directory(libraries_get_path('mpdf'), $pattern)); | |
if (isset($tools[0])) { | |
require_once $tools[0]; | |
return TRUE; | |
} | |
else { | |
return FALSE; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'drupalModuleLibraryExists.php'; | |
function module_mpdf_save_to_file($html) { | |
$uri = FALSE; | |
if (!module_mpdf_exists()) { | |
return $uri; | |
} | |
// The default format is A4, A4-P for portrait. | |
$format = 'A4-P'; | |
// Make a new object. | |
$mpdf = new mPDF('UTF-8', $format); | |
$mpdf->SetCreator(variable_get('site_name', 'Drupal')); | |
$mpdf->WriteHTML($html); | |
// Try to recover from any warnings/errors. | |
ob_clean(); | |
// Prepare the file to be saved. | |
$filename = "myCustomPDFFile.pdf"; | |
$uri = file_stream_wrapper_uri_normalize('public://pdfs'); | |
file_prepare_directory($uri, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); | |
$uri = "{$uri}/{$filename}"; | |
$pdfoutput = $mpdf->Output('', 'S'); | |
file_unmanaged_save_data($pdfoutput, $uri, FILE_EXISTS_REPLACE); | |
return $uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment