简体   繁体   English

DomPdf pdf创建进度

[英]DomPdf pdf creation progress

How can I get the progress of dompdf. 我如何获得dompdf的进度。 I have a lot of html that is being converted into pdf using dompdf. 我有很多使用dompdf转换为pdf的html。 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. 由于HTML占用大量时间,因此我想知道是否有可能获取进度并将其显示给用户。

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 : 我正在使用此代码生成从此处复制的pdf:

$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. 当用户按下执行此代码的链接时,将生成pdf。 The pdf downloads and shows in the downloads section. pdf下载并在“下载”部分显示。

Edit 2: Now I am trying to handle the download with ajax. 编辑2:现在我正在尝试使用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?? 我正在接收数据,当我执行console.log()时可以看到它,但是如何获取要保存在用户下载文件中的文件? Thanks 谢谢

Dompdf does not currently support a means of reporting its progress. Dompdf当前不支持报告其进度的方法。 Your best bet in the meantime might be to use a spinner to let the user know that PDF generation is ongoing. 同时,最好的选择是使用微调器让用户知道正在进行PDF生成。

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. 如果您将PDF作为附件发送,则因为没有事件触发,所以您无法真正知道用户何时接收它。 On the other hand, using pure AJAX is harder still and browser-dependent since it requires certain HTML5 functionality. 另一方面,使用纯AJAX仍然更加困难,并且依赖于浏览器,因为它需要某些HTML5功能。

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. 您可以混合使用这两种方法,方法是使用AJAX触发PDF渲染,将dompdf生成的PDF保存到服务器的文件系统,将文件名返回给客户端,最后重定向到AJAX回调中的文件。

Assuming jQuery + blockUI (for the lazy dev), here's a simplified example. 假设jQuery + blockUI (对于懒惰的开发者),这是一个简化的示例。

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. 当然,如果您真的想使用AJAX来完成所有这一切,那肯定有可能,并且已经存在许多相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM