在 Ajax 调用后,在嵌入标签中预览使用 TCPDF 创建的 pdf 文件 [英] Preview pdf file created with TCPDF in embed tag after Ajax call

查看:55
本文介绍了在 Ajax 调用后,在嵌入标签中预览使用 TCPDF 创建的 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序使用 TCPDF 生成 pdf 文件,并且工作正常.这是在 php 文件中完成的,用 ajax 调用.我正在使用嵌入标签来预览它们:

So my application generates pdf files using TCPDF, and that works fine. That is done inside php file, called with ajax. I'm using embed tag to preview them like:

$.ajax({
url: 'create_pdf_file.php',
success: function(){
    /* https://stackoverflow.com/questions/17083018/jquery-loads-cached-file-inside-ajax-success-function */
    $('#pdf_placeholder embed').attr('src','output/my_file.pdf?v=' + Math.random()');
   }
});

因为许多用户可以同时生成 my_file.pdf,所以可能会出现一个用户预览为另一个用户生成的文件的情况.所以此时我的问题是如何强制 TCPDF 直接输出到该标签中,而不是使用临时文件

Because many users could generate my_file.pdf at the same time, there could be a case when one user will preview a file generated for another user. So my question at this point is how to force TCPDF to output directly into that tag, not using temp file

 Output('my_file','I')

在 ajax 之后不在这里工作.这里有 建议 可以直接回显 pdf,但我不知道该怎么做,或者可以在全部.无论如何都会尝试使用 success(data) 来像 json 一样接收它.

not working here after ajax. There is an advice here to echo pdf directly back, but i don't know how to do that or is it possible at all. Anyway will try with success(data) to receive that like json.

其他解决方法是为每个用户提供会话绑定文件名,但是使用 session_id() 并附加到文件名是否安全?我很可能会以为每个会话生成 UID 结束.

Other workaround is to give session-bond file name for each user, but is using session_id() and appending to the file name is safe? Most probably I will end with generating UID for each session.

欢迎提出任何一般性建议.

Any general advices are welcome.

推荐答案

您应该尝试以一种广泛用于嵌入图像的方式传递 TCPDF 将产生的内容:Data Uri嵌入 Base64 图片然而,这可能会阻塞浏览器 - 我还没有测试过.

You should try passing what TCPDF will produce in a way widely used in embeding images: Data Uri Embedding Base64 Images This however might choke browser - i haven't test this.

我宁愿将 pdf 文件保存在服务器上,将文件名打印到浏览器或生成文件的其他 ID,以便可以在 ajax 请求成功"时读取它.然后冷静地将正确的文件名传递给 Embed 元素.如果您希望更安全地执行此操作,您可以使用已使用的会话专有数据(如会话 cookie 或与服务器上该 cookie 相关联的数据)对其进行编码.您可以将 pdf 文件访问权限绑定到发送请求以生成它的 IP 和导致生成的请求的时间戳.

I would rather save pdf file on server, print filename to browser or other ID of produced file so it could be read with "success" of the ajax request. Then calmly pass proper filename to Embed element. If you wish to do it more safely you can encode it using already used session-exclusive data like session cookie or data that is assosiated with that cookie on a server. You can bond pdf file access to IP that sent request to produce it and timestamp of request that caused production.

这篇关于在 Ajax 调用后,在嵌入标签中预览使用 TCPDF 创建的 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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