dompdf-> stream在joomla中不起作用 [英] dompdf->stream not working in joomla

查看:135
本文介绍了dompdf-> stream在joomla中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Joomla 3组件中使用dompdf,但是它生成的PDf总是损坏.我已经在文本编辑器中打开了PDF文件,发现损坏的原因是它包含了joomla的所有标题,链接,样式表和javascript,即使我一直在传递我所需的html.

我在joomla组件视图(default.php模板)中使用的代码是:

<?php
defined('_JEXEC') or die('Restricted access');
ob_start();
?>
   <div class="qp-div1">
     <table class="table table-bordered">
       <thead>
          <tr>
            <th colspan="2">Account Status</th>
          </tr>
       </thead>
       <tbody>
         <tr>
           <th>Reference</th>
             <td><?php echo $this->userdetails['cardcode']; ?></td>
        ... more html
<?php

require_once('dompdf/autoload.inc.php');
use Dompdf\Dompdf;
$html = ob_get_contents();
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath('');
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('order');
ob_flush();
ob_end_clean();
?>

即使当您运行dompdf-> stream时,即使我只传递了我想要传递给dompdf-> loadHtml的HTML,它最终还是使用了所有的joomla html,包括所有的样式表,以及更多的javascript.

当我使用dompdf-> output()将输出转储到文件中时,该文件实际上是可以的,所以只有使用dompdf-> stream才有问题.

我用于转储pdf的代码已插入到stream命令之前,如下所示:

$file_to_save = '/temp/pdf/file.pdf';
file_put_contents($file_to_save, $dompdf->output()); 
$dompdf->stream('order');

有什么我可以阻止$ dompdf-> stream命令使用所有Joomla html的内容吗?

欢呼

解决方案

尝试在刷新输出之前清除输出缓冲区,如下例所示

<?php
defined('_JEXEC') or die('Restricted access');
   $html = '<div class="qp-div1">
     <table class="table table-bordered">
       <thead>
          <tr>
            <th colspan="2">Account Status</th>
          </tr>
       </thead>
       <tbody>
         <tr>
           <th>Reference</th>
             <td>'.$this->userdetails['cardcode'].'</td>
         </tr>
        </tbody>
      </table>';
require_once('dompdf/autoload.inc.php');
ob_clean();
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath('');
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('order');
ob_end_flush();
?>

I'm trying to use dompdf in my Joomla 3 component, but the PDf that it generates to download is always corrupt. I have opened the PDF file in a text editor and found that the reason for the corruption is that it is including all the headers, links, stylesheets and javascript from joomla, even though I'm ionly passing teh html that i need.

The code I'm using in my joomla component view (the default.php template) is :

<?php
defined('_JEXEC') or die('Restricted access');
ob_start();
?>
   <div class="qp-div1">
     <table class="table table-bordered">
       <thead>
          <tr>
            <th colspan="2">Account Status</th>
          </tr>
       </thead>
       <tbody>
         <tr>
           <th>Reference</th>
             <td><?php echo $this->userdetails['cardcode']; ?></td>
        ... more html
<?php

require_once('dompdf/autoload.inc.php');
use Dompdf\Dompdf;
$html = ob_get_contents();
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath('');
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('order');
ob_flush();
ob_end_clean();
?>

Even though I was passing only the HTML I wanted to dompdf->loadHtml when you ran dompdf->stream it ended up using all the joomla html including all teh stylesheets, javascript anf more.

When I use dompdf->output() to dump the output to a file the file is actually okay, so it is only the use of dompdf->stream that has the issue.

The code I used to dump the pdf was inserted before the stream command and looked like this :

$file_to_save = '/temp/pdf/file.pdf';
file_put_contents($file_to_save, $dompdf->output()); 
$dompdf->stream('order');

Is there anything I can do to stop the $dompdf->stream command from using includding all the Joomla html ?

Cheers

解决方案

Try clearing output buffer before flushing output like in the example below

<?php
defined('_JEXEC') or die('Restricted access');
   $html = '<div class="qp-div1">
     <table class="table table-bordered">
       <thead>
          <tr>
            <th colspan="2">Account Status</th>
          </tr>
       </thead>
       <tbody>
         <tr>
           <th>Reference</th>
             <td>'.$this->userdetails['cardcode'].'</td>
         </tr>
        </tbody>
      </table>';
require_once('dompdf/autoload.inc.php');
ob_clean();
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath('');
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('order');
ob_end_flush();
?>

这篇关于dompdf-&gt; stream在joomla中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆