Home > Software engineering >  How to see generated pdf on localhost webpage in Golang?
How to see generated pdf on localhost webpage in Golang?

Time:10-13

I am trying to see my generated pdf on http://localhost:3333/pdf.

I am trying to learn generating pdf using golang. Tying to make the changes in pdf and see live on localhost server ( http://localhost:3333/pdf ). But I am clueless that how can I show pdf on localhost.

My Code::

package main

import (
    "errors"
    "fmt"
    "io"
    "net/http"
    "os"

    "github.com/krazybee/gofpdf"
)

type bufWriter struct {
    buf []byte
}

func getRoot(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("got / request\n")
    io.WriteString(w, "This is my website!\n")
}

func getHello(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("got /hello request\n")
    io.WriteString(w, "Hello, HTTP!\n")
}

func generatePDF(w http.ResponseWriter, r *http.Request) {
    pw := new(bufWriter)
    pdf := gofpdf.New("P", "mm", "A4", "")
    fontsize := 12.0

    font := "Arial"

    pdf.SetFont("Arial", "", 6)

    pdf.AddPage()
    pdf.SetMargins(5, 5, 5)
    pdf.SetAutoPageBreak(true, 34)

    pdf.SetFont(font, "B", fontsize)

    pdf.MultiCell(0, 8, "TERMS OF LIVING", "", "C", false)
    pdf.MultiCell(0, 8, "Terms", "", "C", false)

    // Show the pdf on localhost web page.
    err = pdf.OutputAndClose(pw)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("PDF served successfully")
}

func main() {
    http.HandleFunc("/", getRoot)
    http.HandleFunc("/hello", getHello)
    http.HandleFunc("/pdf", generatePDF)

    err := http.ListenAndServe(":3333", nil)
    if errors.Is(err, http.ErrServerClosed) {
        fmt.Printf("server closed\n")
    } else if err != nil {
        fmt.Printf("error starting server: %s\n", err)
        os.Exit(1)
    }
}

This is what I have tried. But from the comment // Show the pdf on the localhost web page. I don't know how to proceed further after that.

Can someone help me how can I do that? How can I see the generated pdf on a localhost web page?

CodePudding user response:

By default http package serves the page with Content-type: text/html header. You have to specify the needed header by yourself before writing the page content:

w.Header().Set("Content-Type", "application/pdf")

However, the sample code you provided does not work, because a mess with io streams. Here is what you need to change:

func generatePDF(w http.ResponseWriter, r *http.Request) {
    var b bytes.Buffer
    pw := io.Writer(&b)
    pr := io.Reader(&b)
    ...
    err := pdf.Output(pw)
    ...
    w.Header().Set("Content-Type", "application/pdf")
    resPDF, _ := ioutil.ReadAll(pr)
    w.Write(resPDF)
    fmt.Println("PDF served successfully")
}

I suggest you to have some practice with io library.

Here is the full code that works for me:

package main

import (
    "bytes"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "os"

    "github.com/krazybee/gofpdf"
)

func getRoot(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("got / request\n")
    io.WriteString(w, "This is my website!\n")
}

func getHello(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("got /hello request\n")
    io.WriteString(w, "Hello, HTTP!\n")
}

func generatePDF(w http.ResponseWriter, r *http.Request) {
    var b bytes.Buffer
    pw := io.Writer(&b)
    pr := io.Reader(&b)

    pdf := gofpdf.New("P", "mm", "A4", "")
    fontsize := 12.0

    font := "Arial"

    pdf.SetFont("Arial", "", 6)

    pdf.AddPage()
    pdf.SetMargins(5, 5, 5)
    pdf.SetAutoPageBreak(true, 34)

    pdf.SetFont(font, "B", fontsize)

    pdf.MultiCell(0, 8, "TERMS OF LIVING", "", "C", false)
    pdf.MultiCell(0, 8, "Terms", "", "C", false)

    // Show the pdf on localhost web page.
    err := pdf.Output(pw)
    if err != nil {
        fmt.Println(err)
        return
    }
    w.Header().Set("Content-Type", "application/pdf")
    resPDF, _ := ioutil.ReadAll(pr)
    w.Write(resPDF)
    fmt.Println("PDF served successfully")
}

func main() {
    http.HandleFunc("/", getRoot)
    http.HandleFunc("/hello", getHello)
    http.HandleFunc("/pdf", generatePDF)

    err := http.ListenAndServe(":3333", nil)
    if errors.Is(err, http.ErrServerClosed) {
        fmt.Printf("server closed\n")
    } else if err != nil {
        fmt.Printf("error starting server: %s\n", err)
        os.Exit(1)
    }
}
  •  Tags:  
  • go
  • Related