-4

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)
Sumit Rathore
  • 571
  • 2
  • 7
  • 19
  • 1
    **Never ignore errors**!! What are you ignoring from `GetFile`? Does `SaveToFile` return errors? You ignore `ioutil.ReadFile` errors. You ignore `ioutil.WriteFile` errors. And using files at all here is just wrong. – Dave C Apr 19 '15 at 13:15

3 Answers3

1

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

JimB
  • 104,193
  • 13
  • 262
  • 255
  • thanks for your answer. I get your point that storing files in db have performance issue.But my problem was how to make pdf file and download it after reading content from db. any help regarding how to do it? – Sumit Rathore Jan 11 '15 at 01:58
  • The database is beside the point. There's no reason to write a file if you're just sending the data off somewhere else. – JimB Jan 11 '15 at 02:35
  • @JimB You last comment was useless. What gives you the right to make decisions for the OP. He asked a legitimate question and has a need for it. Giving a suggestion is fine, then answer the question. – Donald French Jun 14 '19 at 18:56
  • @DonaldFrench: I did answer to the question, (albeit over 4 years ago, with limited info from the OP). The problem is ignoring the `multipart.File`, which contains the file, which is why it has nothing to do with the db and the rest doesn't work. Everything else is just suggestion as you say. – JimB Jun 14 '19 at 19:08
0

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.

Donald French
  • 1,731
  • 1
  • 17
  • 30
-1

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())
}
Rio
  • 851
  • 10
  • 5