简体   繁体   English

在通过AJAX动态添加一些内容后,如何将当前页面转换为pdf?

[英]How can I convert my current page to pdf after some content dynamically added via AJAX?

I have found some libraries or web services in PHP that does the job. 我在PHP中找到了一些可以完成这项工作的库或Web服务。 The problem is that the conversion is done when the page is fully loaded, I would like to convert the page to PDF after some content dynamically added via AJAX in onload event. 问题是转换是在页面完全加载时完成的,我想在onload事件中通过AJAX动态添加一些内容之后 将页面转换为PDF

Thank you very much, Omar 非常感谢,奥马尔

您可以将document.getElementsByTagName('html')[0].innerHTML回服务器(可能使用AJAX)并从中生成PDF。

When you add content with AJAX, that's only happening on the client's machine, so the server-side PHP conversion code will not work with it. 当您使用AJAX添加内容时,这只发生在客户端的计算机上,因此服务器端的PHP转换代码将无法使用它。 You'll need to come up with a server-side method of including the dynamic content if you want to do the PDF creation on the server. 如果要在服务器上进行PDF创建,则需要提供包含动态内容的服务器端方法。

You could possibly implement an AJAX call that sent the page content/state back to serverside, after the dynamic contens is added, where it could be rendered as a pdf. 在添加动态匹配之后,您可以实现将页面内容/状态发送回服务器端的AJAX调用,其中可以将其呈现为pdf。 You might not need all the page, depending on where this "dynamic content" is going to go. 您可能不需要所有页面,具体取决于此“动态内容”的去向。

Seems a bit messy, but without knowing the project it's hard to say whether there could be a cleaner method to do what you're trying to do. 看起来有点混乱,但是在不知道项目的情况下,很难说是否可以采用更清洁的方法来做你想做的事情。

This is a client-side requirement due to the Ajax situation. 由于Ajax情况,这是客户端要求。 If they're using a Mac they can simply save as PDF but otherwise you have little control over what they can do. 如果他们使用Mac,他们可以简单地保存为PDF,但除此之外你几乎无法控制他们可以做什么。

What you can do however for a server-side implementation is keep a record of the page and all their AJAX requests since loading that page, and construct the HTML and PDF on the server from that record if it is requested. 但是,对于服务器端实现,您可以做的是保存页面及其加载该页面后的所有AJAX请求的记录,并在请求时从该记录构建服务器上的HTML和PDF。 Of course this is not a simple task and quite overkill. 当然,这不是一项简单的任务,而且相当矫枉过正。 You'd probably be better off having a different mechanism to create a server-side PDF report of a page other than turning HTML into PDF. 除了将HTML转换为PDF之外,您可能最好使用不同的机制来创建页面的服务器端PDF报告。

If your page can be updated solely on the client with no trip to the server, you'll have to post back up to your app. 如果您的页面只能在客户端上更新而无需访问服务器,则必须回发到您的应用程序。 That way you'll have all the content and will not have rebuild, which would not be possible if there are client side only interactions. 这样你就拥有了所有的内容,并且不会重建,如果只有客户端的交互那么这是不可能的。

Wow, thank you everyone I didn't know that this is comunity is so active. 哇,谢谢大家我不知道这是社区如此活跃。 To answer Beepcake about the project: 要回答有关该项目的Beepcake:

When the page loads the app retrieves, from more than 40 servers, biological information via AJAX request, then a unique view is displayed where you can manipulate the graphic with many options. 当页面加载应用程序时,通过AJAX请求从40多台服务器检索生物信息,然后会显示一个独特的视图,您可以使用许多选项操作图形。

So, the cool thing will be to print when the user makes his own version of the graphic. 因此,当用户制作自己的图形版本时,很酷的事情就是打印。 I think that the best solution is to POST the entire HTML with document.getElementsByTagName('html')[0].innerHTML as RoBorg has said and then generate the PDF with a library such as dompdf 我认为最好的解决办法是用POST document.getElementsByTagName(“HTML”)的整个HTML [0] .innerHTML作为RoBorg所说然后生成与库如DOMPDF的PDF

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

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