How to make pdf file by reading content from database using beego?

huangapple go评论81阅读模式
英文:

How to make pdf file by reading content from database using beego?

问题

我阅读了一个PDF文件,然后将其原始内容存储到数据库中。现在我想从数据库中读取该内容并创建一个PDF文件,以便用户可以下载。

为此,我读取内容并将其写入带有.pdf扩展名的文件中。但结果是一个空的PDF文件。

我这样做是因为我想避免将文件保存在磁盘上。

我正在使用beego框架。

有什么建议/帮助吗?

以下是我正在做的事情:

从PDF文件中读取并将其写入数据库:

_, header, _ := c.GetFile("attachment[]")
attachment := header.Filename
c.SaveToFile("attachment[]", "/tmp/"+attachment)
content, _ := ioutil.ReadFile("/tmp/" + attachment)
s := string(content)

然后将这个's'写入数据库

从数据库中读取并写入PDF文件:

err := o.Raw("SELECT attachment FROM table_name WHERE id=?", id).QueryRow(&att)
if err == nil {
     fmt.Println(att)
}
var data []byte
data = []byte(att)
ioutil.WriteFile("/tmp/hello.pdf", data, 0666)
英文:

I read a pdf file and then store its raw content into database. Now i want to read that content from database and create a pdf so that user can download it.

For this i read the content and write it to file with .pdf extension. But the result in an empty pdf file.

I am doing this because i want to avoid saving files in disk.

I am using beego framework.

Any suggestions/help?

Here what i am doing

Reading from pdf file and writing it to database

_,header,_:=c.GetFile("attachment[]")
attachment:=header.Filename
c.SaveToFile("attachment[]","/tmp/"+attachment)
content,_:= ioutil.ReadFile("/tmp/"+attachment)
s := string(content)

then write this 's' to database

reading from database and writing to pdf file

err := o.Raw("SELECT attachment FROM table_name WHERE id=?",id).QueryRow(&att)
if err == nil {
     fmt.Println(att)
}
var data []byte
data = []byte(att)
ioutil.WriteFile("/tmp/hello.pdf",data, 0666)

答案1

得分: 1

你正在忽略GetFile调用中的multipart.File。直接使用它即可,将其写入磁盘再读取回来是浪费时间。

在从数据库读取文件并发送给客户端之间,也没有必要将文件保存到磁盘上。

(你可能需要确保你的数据库经过优化,以处理这样的大型二进制对象。这对于大多数数据库来说不是常见的用例。)

英文:

You're ignoring the multipart.File from the GetFile call. Use that directly. It's a waste of time to write it to disk and read it back again.

There's also no reason to save the file to disk between reading it from the DB and sending to the client.

(You may want to be sure that your DB is optimized to handle large blobs like this. It's not the usual use-case for most databases.)

答案2

得分: 0

我遇到了一个类似的问题,需要在MongoDB文档中存储一个PDF文件。我将其作为Base64加密的字符串值进行存储。我的步骤如下:

  1. 找到记录。

  2. 解码字符串:

  3. 从pdfBytes创建一个*Buffer

  4. 直接将其写入http.ResponseWriter:

    b := bytes.NewBuffer(pdfBytes) // 步骤2 pdfBytes,err := b64.StdEncoding.DecodeString(doc.Image) // 步骤3 if _, err := b.WriteTo(w); err != nil { // 步骤4 fmt.Fprintf(w, "%s", err) // 处理错误 }

这个方法运行良好,我使用它处理了4000多万个文档。然而,我一直对是否有更好的方法感兴趣。正如@JimB所说,如果需要从文件中读取,他的方法是最好的。问题是问题描述得非常模糊。

英文:

I had the same issue of a pdf in a MongoDB document. It was stored as a string value Base64 encrypted. My steps were:

  1. Find the record.

  2. Decode the string:

  3. Create a *Buffer from the pdfBytes

  4. write it directly to the http.ResponseWritter:

    b := bytes.NewBuffer(pdfBytes) // Step 2
    pdfBytes,err := b64.StdEncoding.DecodeString(doc.Image) // Step 3
    if _, err := b.WriteTo(w); err != nil { // Step 4
    fmt.Fprintf(w, "%s", err) // Handle the error
    }

This works fine, I handle 40+ million documents using it. However, I am always interested in learning if there is a better way. As @JimB said, if you are required to read from a file his way is best. The problem is the question was very vague.

答案3

得分: -1

当使用github.com/jung-kurt/gofpdf库时:

func (c *MyController) PrintPDF() {         
    pdf := gofpdf.New("", "", "", "")
    pdf.AddPage()
    pdf.Text(10, 20, "Circles")
    var b bytes.Buffer
    w := bufio.NewWriter(&b)
    pdf.Output(w)
    pdf.Close()
    w.Flush()
    c.Ctx.Output.ContentType("application/pdf")
    c.Ctx.Output.Body(b.Bytes())
}
英文:

When is used github.com/jung-kurt/gofpdf library:

func (c *MyController) PrintPDF() {         
    pdf := gofpdf.New("", "", "", "")
    pdf.AddPage()
    pdf.Text(10, 20, "Circles")
    var b bytes.Buffer
    w := bufio.NewWriter(&b)
    pdf.Output(w)
    pdf.Close()
    w.Flush()
	c.Ctx.Output.ContentType("application/pdf")
	c.Ctx.Output.Body(b.bytes())
}

huangapple
  • 本文由 发表于 2015年1月7日 15:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/27814336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定