WKHtmlToPDF::generatePDF PHP Method

generatePDF() public method

public generatePDF ( $imageDirectory, $prefix, $suffix, $html, $output_html = false, $inject_autoprint_js = true )
    public function generatePDF($imageDirectory, $prefix, $suffix, $html, $output_html = false, $inject_autoprint_js = true)
    {
        !$output_html && ($html = $this->remapAssetPaths($html));
        !$output_html && ($html = $this->remapCanvasImagePaths($html));
        $this->findOrCreateDirectory($imageDirectory);
        $html_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}_{$suffix}.html" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}.html";
        $pdf_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}_{$suffix}.pdf" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}.pdf";
        $footer_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "footer_{$suffix}.html" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . 'footer.html';
        $this->writeFile($html_file, $html);
        $footer = $this->formatFooter($this->readFile(Yii::app()->basePath . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'print' . DIRECTORY_SEPARATOR . 'pdf_footer.php'), $this->left, $this->middle, $this->right, $this->patients, $this->barcodes, $this->docrefs);
        $this->writeFile($footer_file, $footer);
        if ($output_html) {
            echo $html . $footer;
            return true;
        }
        $top_margin = $this->top_margin ? '-T ' . $this->top_margin : '';
        $bottom_margin = $this->bottom_margin ? '-B ' . $this->bottom_margin : '';
        $left_margin = $this->left_margin ? '-L ' . $this->left_margin : '';
        $right_margin = $this->right_margin ? '-R ' . $this->right_margin : '';
        $nice = Yii::app()->params['wkhtmltopdf_nice_level'] ? 'nice -n' . Yii::app()->params['wkhtmltopdf_nice_level'] . ' ' : '';
        $res = $this->execute($nice . escapeshellarg($this->wkhtmltopdf) . ' --footer-html ' . escapeshellarg($footer_file) . " --print-media-type {$top_margin} {$bottom_margin} {$left_margin} {$right_margin} " . escapeshellarg($html_file) . ' ' . escapeshellarg($pdf_file) . ' 2>&1');
        if (!$this->fileExists($pdf_file) || $this->fileSize($pdf_file) == 0) {
            if ($this->fileSize($pdf_file) == 0) {
                $this->deleteFile($pdf_file);
            }
            throw new Exception("Unable to generate {$pdf_file}: {$res}");
        }
        $this->deleteFile($html_file);
        $this->deleteFile($footer_file);
        if ($pdf = $this->getPDFOptions($pdf_file)) {
            if ($inject_autoprint_js) {
                $pdf->injectJS('print(true);');
            }
            $pdf->disablePrintScaling();
            $pdf->write();
        }
        return true;
    }

Usage Example

 /**
  * create the PDF file as a ProtectedFile for the given side.
  *
  * @param CController $controller
  * @param array       $template_data
  * @param string      $side
  *
  * @throws Exception
  *
  * @return ProtectedFile|null
  */
 protected function createAndSavePdfForSide(CController $controller, array $template_data, $side)
 {
     if ($html = $this->getPDFContentForSide($controller, $template_data, $side)) {
         $html = '<link rel="stylesheet" type="text/css" href="' . $controller->assetPath . '/css/print.css" />' . "\n" . $html;
         $this->event->lock();
         if (!$this->event->hasPDF('therapy_application')) {
             $wk = new WKHtmlToPDF();
             $wk->setDocuments(1);
             $wk->setDocRef($this->event->docref);
             $wk->setPatient($this->event->episode->patient);
             $wk->setBarcode($this->event->barcodeHTML);
             $wk->generatePDF($this->event->imageDirectory, 'event', 'therapy_application', $html, false, false);
         }
         $this->event->unlock();
         if (@$_GET['html']) {
             return Yii::app()->end();
         }
         $pfile = ProtectedFile::createForWriting('ECForm - ' . $side . ' - ' . $template_data['patient']->hos_num . '.pdf');
         if (!@copy($this->event->getPDF('therapy_application'), $pfile->getPath())) {
             throw new Exception('Unable to write to file: ' . $pfile->getPath());
         }
         if (!$pfile->save()) {
             throw new Exception('Unable to save file: ' . print_r($pfile->errors, true));
         }
         return $pfile;
     }
     return;
 }
All Usage Examples Of WKHtmlToPDF::generatePDF