-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/jaytaylor/html2text"
"github.com/jung-kurt/gofpdf"
)
func main() {
http.HandleFunc("/generate-pdf", htmlToPdfHandler)
fmt.Println("Server listening on port 8080...")
http.ListenAndServe(":8080", nil)
}
func htmlToPdfHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// Read the request body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
// Close the request body after reading (important!)
defer r.Body.Close()
generatePdf(string(body))
// modify to save to s3 and return url
w.Write([]byte("PDF generated successfully."))
}
func generatePdf(htmlContent string) {
// Convert HTML to plain text
plainText, err := html2text.FromString(htmlContent)
if err != nil {
fmt.Println("Error converting HTML to plain text:", err)
return
}
// Generate PDF
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.MultiCell(0, 10, plainText, "", "", false)
// Save the PDF to a file
err = pdf.OutputFileAndClose("output.pdf")
if err != nil {
fmt.Println("Error saving PDF:", err)
return
}
fmt.Println("PDF generated successfully.")
}