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

时间:2022-12-29 21:15:07

I have found some libraries or web services in PHP that does the job. 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.

我在PHP中找到了一些可以完成这项工作的库或Web服务。问题是转换是在页面完全加载时完成的,我想在onload事件中通过AJAX动态添加一些内容之后将页面转换为PDF。

Thank you very much, Omar

非常感谢,奥马尔

6 个解决方案

#1


5  

You could post back document.getElementsByTagName('html')[0].innerHTML to the server (possibly using AJAX) and generate a PDF from that.

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

#2


4  

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. 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.

当您使用AJAX添加内容时,这只发生在客户端的计算机上,因此服务器端的PHP转换代码将无法使用它。如果要在服务器上进行PDF创建,则需要提供包含动态内容的服务器端方法。

#3


2  

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. You might not need all the page, depending on where this "dynamic content" is going to go.

在添加动态匹配之后,您可以实现将页面内容/状态发送回服务器端的AJAX调用,其中它可以呈现为pdf。您可能不需要所有页面,具体取决于此“动态内容”的去向。

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.

看起来有点乱,但不知道该项目,很难说是否有更干净的方法来做你想做的事情。

#4


1  

This is a client-side requirement due to the Ajax situation. If they're using a Mac they can simply save as PDF but otherwise you have little control over what they can do.

由于Ajax情况,这是客户端要求。如果他们使用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. 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.

但是,对于服务器端实现,您可以做的是保存页面及其加载该页面后的所有AJAX请求的记录,并在请求时从该记录构建服务器上的HTML和PDF。当然,这不是一项简单的任务,而且相当矫枉过正。除了将HTML转换为PDF之外,您可能最好使用不同的机制来创建页面的服务器端PDF报告。

#5


1  

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.

如果您的页面只能在客户端上更新而无需访问服务器,则必须回发到您的应用程序。这样你就拥有了所有的内容,而且不会重建,如果只有客户端的交互那么这是不可能的。

#6


1  

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

因此,当用户制作自己的图形版本时,很酷的事情就是打印。我认为最好的解决方案是使用document.getElementsByTagName('html')[0] .innerHTML对整个HTML进行POST,如RoBorg所说,然后使用诸如dompdf之类的库生成PDF

#1


5  

You could post back document.getElementsByTagName('html')[0].innerHTML to the server (possibly using AJAX) and generate a PDF from that.

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

#2


4  

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. 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.

当您使用AJAX添加内容时,这只发生在客户端的计算机上,因此服务器端的PHP转换代码将无法使用它。如果要在服务器上进行PDF创建,则需要提供包含动态内容的服务器端方法。

#3


2  

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. You might not need all the page, depending on where this "dynamic content" is going to go.

在添加动态匹配之后,您可以实现将页面内容/状态发送回服务器端的AJAX调用,其中它可以呈现为pdf。您可能不需要所有页面,具体取决于此“动态内容”的去向。

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.

看起来有点乱,但不知道该项目,很难说是否有更干净的方法来做你想做的事情。

#4


1  

This is a client-side requirement due to the Ajax situation. If they're using a Mac they can simply save as PDF but otherwise you have little control over what they can do.

由于Ajax情况,这是客户端要求。如果他们使用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. 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.

但是,对于服务器端实现,您可以做的是保存页面及其加载该页面后的所有AJAX请求的记录,并在请求时从该记录构建服务器上的HTML和PDF。当然,这不是一项简单的任务,而且相当矫枉过正。除了将HTML转换为PDF之外,您可能最好使用不同的机制来创建页面的服务器端PDF报告。

#5


1  

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.

如果您的页面只能在客户端上更新而无需访问服务器,则必须回发到您的应用程序。这样你就拥有了所有的内容,而且不会重建,如果只有客户端的交互那么这是不可能的。

#6


1  

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

因此,当用户制作自己的图形版本时,很酷的事情就是打印。我认为最好的解决方案是使用document.getElementsByTagName('html')[0] .innerHTML对整个HTML进行POST,如RoBorg所说,然后使用诸如dompdf之类的库生成PDF