weixin_33725722 2016-10-10 16:03 采纳率: 0%
浏览 33

DomPdf pdf创建进度

How can I get the progress of dompdf. I have a lot of html that is being converted into pdf using dompdf. Because the HTML is a lot it takes a while so I want to know if its possible to get the progress and display it to the user.

Edit:

According to suggestion I have decided to use a spinner. But I need to know when the file is received by the user. I'm using this code to generate the pdf which is copied from here:

$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

The pdf is generated when a user presses a link that executes this code. The pdf downloads and shows in the downloads section.

Edit 2: Now I am trying to handle the download with ajax. I am receiving the data and I can see it when I do console.log() but how do I get the file to be saved in the users downloads?? Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2016-10-12 14:43
    关注

    Dompdf does not currently support a means of reporting its progress. Your best bet in the meantime might be to use a spinner to let the user know that PDF generation is ongoing.

    If you're sending the PDF as an attachment you can't really know when the user receives it since there's no event triggered. On the other hand, using pure AJAX is harder still and browser-dependent since it requires certain HTML5 functionality.

    You can use a mix of the two methods by using AJAX to trigger PDF rendering, saving the dompdf-generated PDF to the server's file system, returning the filename to the client, and finally redirecting to the file in the AJAX callback.

    Assuming jQuery + blockUI (for the lazy dev), here's a simplified example.

    First the client code:

    <script>
    $('.pdf').click( function {evt} (
      $.blockUI();
      $.get('generate_pdf.php', function (data) {
        $.unblockUI();
        location.href = data;
      });
    ));
    </script>
    
    <button class="pdf">Generate PDF</button>
    

    and the server:

    <?php
    // require dompdf autoloader, then...
    $dompdf = new Dompdf();
    $dompdf->loadHtml('hello world');
    $dompdf->render();
    $filename = sprintf('pdf_%s.pdf', date('ymd'));
    file_put_contents($filename, $dompdf->output());
    echo $filename;
    ?>
    

    You'll need to craft the code to meet your needs. And of course, you can make it more robust by building in more error handling. Plus you'd want some kind of clean-up to remove old files.


    Of course, if you really want to do this all using AJAX it's certainly possible and there are a number of relevant questions already.

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型