Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created March 27, 2013 18:41
Show Gist options
  • Select an option

  • Save larscwallin/5256901 to your computer and use it in GitHub Desktop.

Select an option

Save larscwallin/5256901 to your computer and use it in GitHub Desktop.

Revisions

  1. larscwallin created this gist Mar 27, 2013.
    86 changes: 86 additions & 0 deletions simplx.dompdf.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    <?php
    /*
    simplx.dompdf
    */


    $filename = isset($filename) ? $filename : false;
    $outputto = isset($outputto) ? $outputto : 'file';
    $directory = isset($directory) ? $directory : $modx->getOption('assets_path');
    $html = isset($html) ? $html : false;
    $papersize = isset($papersize) ? $papersize : 'a4';
    $orientation = isset($orientation) ? $orientation : 'portrait';;

    $success = false;
    $domPdf;

    function writePdf($html, $filename, $outputto, $directory) {
    global $modx, $domPdf;

    if (!isset($domPdf)) {
    // Include the class
    include(($modx->getOption('core_path').'components/simplx/dompdf/dompdf_config.inc.php'));
    $domPdf = new DOMPDF();

    $domPdf->set_paper($papersize, $orientation);

    if(!$domPdf){
    $modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Unable to instantiate domPdf Object');
    return false;
    }

    }

    $domPdf->load_html($html);
    $domPdf->render();

    switch($outputto) {
    //case 'header':
    // $this->domPdf->stream($fileName);
    // break;
    case 'file':

    if(!is_dir($directory)){
    // This directory does not exist so we return false.
    $modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Directory "'.$directory.'" does not exist.');
    return false;
    }

    $output = $domPdf->output();

    $success = file_put_contents(($directory.$filename), $output);

    if($success){
    return true;
    }else{
    return false;
    }

    break;

    case 'string':
    $output = $domPdf->output();

    return $output;
    break;
    }

    return true;
    }

    if($html && $filename){
    $success = writePdf($html,$filename,$outputto,$directory);

    if($success){
    return true;
    }else{
    $modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Unable to create file "'.$fileName.'".');
    return false;
    }

    }else{
    $modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Parameters "html" or "fileName" are missing or empty.');
    return false;
    }