英文:
How to see generated pdf on localhost webpage in Golang?
问题
我正在尝试在 http://localhost:3333/pdf 上查看生成的 PDF。
我正在尝试使用 Golang 生成 PDF。试图对 PDF 进行更改并在本地服务器上实时查看(http://localhost:3333/pdf)。但是我不知道如何在本地主机上显示 PDF。
我的代码:
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("收到 / 请求\n")
io.WriteString(w, "这是我的网站!\n")
}
func getHello(w http.ResponseWriter, r *http.Request) {
fmt.Printf("收到 /hello 请求\n")
io.WriteString(w, "你好,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)
// 在本地主机的网页上显示 PDF。
err := pdf.OutputAndClose(pw)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("PDF 服务成功")
}
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("服务器已关闭\n")
} else if err != nil {
fmt.Printf("启动服务器时出错:%s\n", err)
os.Exit(1)
}
}
这是我尝试的内容。
但是从注释 // 在本地主机的网页上显示 PDF。
开始,我不知道如何继续进行。
有人可以帮助我吗?我该如何在本地主机的网页上查看生成的 PDF?
英文:
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?
答案1
得分: 1
默认情况下,http包使用Content-Type: text/html头来提供页面。在编写页面内容之前,您需要自己指定所需的头部:
w.Header().Set("Content-Type", "application/pdf")
然而,您提供的示例代码不起作用,因为io流混乱。以下是您需要更改的内容:
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")
}
我建议您多练习一下io库。
以下是对我有效的完整代码:
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)
// 在本地主机网页上显示pdf。
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)
}
}
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论