从数据库加载缓冲区数据为 PDF [英] Loading buffer data from database as PDF

查看:26
本文介绍了从数据库加载缓冲区数据为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个网络应用程序,用户可以在其中上传 PDF 文件,然后检索并查看它.为了实现这一点,我一直在做的是将 PDF 作为 bytea 数据类型上传到 PostgreSQL 数据库(该列称为附件"),然后让 nodeJS 提供这些数据以供取回并转换回 PDF 以查看.

但是,我一直在努力将数据转换回有效的 PDF.这是我目前使用的方法.

var file = new Blob([res[i].attachment], { type: 'application/pdf' });var fileURL = URL.createObjectURL(file);document.getElementById("pdf_box").data = fileURL;

#pdf_box 标识符指的是 HTML 文件中用于显示 PDF 的对象元素(当我将虚拟 PDF 文件的文件位置提供给该元素的数据属性时,这已被证明有效).

res[i].attachment 还显示以 JSON 格式提供有效的缓冲区数据,下面提供了一个示例:

"attachment":{"type":"Buffer","data":[91,111,98,106,101,99,116,32,70,105,108,101,93]}

然而,当我将创建的 fileURL 加载到 #pdf_box 的数据属性中时,我收到一条错误消息,指示无效 PDF 文件的行.到目前为止,我的研究似乎表明这可能是因为数据是作为缓冲区进入的,而它需要以字节形式出现,但我没有找到太多可以帮助我展示如何实现这一点或是否有办法在具有我有权访问的数据的表单之间进行转换?我也偶尔看到一个名为 pdf.create() 的方法的引用,但我找不到关于此的文档,我认为它必须属于第三方 JS 库.

如果您知道任何信息可以帮助我了解我的问题是什么以及需要搜索什么来找出解决方案,我们将不胜感激,谢谢.

解决方案

对于所有阅读此问题的人来说,这种方法似乎不太可能,而且实施起来可能效率极低.如果您向 nodeJS 发送一个字符串,该字符串大于您到该服务器的链接的 MTU(在 68 字节和 >1500 字节之间的任何位置,具体取决于您网络的每个组件),则数据包将被静默丢弃,不会尝试解决或抛出错误.你必须做的是采取使用multipart/form-data"的方法.编码以将任何文件发送到服务器(在此处找到有关此内容的更多信息).>

还应该提到的是,不建议在任何容量下将文件上传到数据库(因为数据库对于大量数据的存储效率非常低),应该做的是上传文件路径的引用或用于查找文件的 URL.然后在客户端,当您检索文件位置时,该文件可以这样呈现...

要更改数据属性,可以进行以下操作...

document.getElementById("pdf").data = "../files/test2.pdf";

I have been developing a web app where the user can upload a PDF file, and then later retrieve it and view it. What I have been doing to achieve this is having the PDF uploaded to a PostgreSQL database as bytea datatype (the column is called "attachment"), and then having nodeJS offer up this data to be fetched back and turned back into a PDF to view.

However I have been struggling to convert the data back into a valid PDF. This is the method I am using so far.

var file = new Blob([res[i].attachment], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
document.getElementById("pdf_box").data = fileURL;

The #pdf_box identifier refers to an object element in a HTML file which is used to display the PDF (this has been shown to work when I provide the file location of a dummy PDF file to the data attribute of this element).

The res[i].attachment is also shown to provide valid buffer data in JSON format, with an example provided below:

"attachment":{"type":"Buffer","data":[91,111,98,106,101,99,116,32,70,105,108,101,93]}

When I load the created fileURL into the data attribute of #pdf_box however, I get an error indicating along the lines of an invalid PDF file. My research so far appears to indicate this may be because the data is coming in as buffer whereas it needs to be in byte form, but I haven't found much that helps show me how this may be achieved or if there is a way to convert between the forms with the data I have access to? I have also seen occasional reference to a method called pdf.create(), but I cannot find documentation on this and I assume it must belong to a third-party library for JS.

If you know of any information that can help me with understanding what my problem is and what to search to figure out a solution, it would all be greatly appreciated, thank you.

解决方案

To all reading this question, this method does not seem possible and would likely be incredibly inefficient to implement. If you send a string to nodeJS larger than the MTU of your link to this server (anywhere between 68 bytes and >1500 bytes depending on every component of your network) the packet will be silently dropped with no attempts to resolve or throw an error. What you must do is take the approach of using "multipart/form-data" encoding to send any files to the server (more on this found here).

It should also be mentioned that uploading a file to the database is not recommended in any capacity (due to databases being quite inefficient storage for large amounts of data) and what should be done is to upload a reference to the file path or URL to find the file at. Then on the client-side this file could be rendered as such when you have retrieved the file location...

<object id="pdf" data="../files/test.pdf"></object>

To change the data attribute, the following can be done...

document.getElementById("pdf").data = "../files/test2.pdf";

这篇关于从数据库加载缓冲区数据为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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