├── .gitignore ├── .travis.yml ├── LICENSE ├── README.MD ├── SECURITY.md ├── go.mod ├── go.sum ├── html-pdf-new.gif ├── main.go ├── pdfGenerator └── pdf.go ├── storage └── example.pdf └── templates └── sample.html /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: 4 | - "1.16" 5 | 6 | os: 7 | - linux 8 | 9 | dist: trusty 10 | sudo: false 11 | install: true 12 | 13 | script: 14 | - unset GOPATH 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 MindInventory 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # Golang HTML to PDF Converter 2 | 3 | 4 | 5 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/mindinventory/Golang-HTML-TO-PDF-Converter/blob/master/LICENSE) 6 | 7 | For reading any document, one prefers PDF format over any other formats as it is considered as a standard format for any document in our day to day life. We often come across converting any form details or previews on the website in the form of HTML to PDF. For GoLang, understanding such usability, Developers at Mindinventory made an effort for making one HTML to PDF Converter which can be used for converting HTML content to PDF. 8 | 9 | 10 | ## What it is? 11 | 12 | It is a repository, integrating which one can offer HTML to PDF Conversion to their customers. 13 | 14 | ## Prerequisite 15 | 16 | Install wkhtmltopdf using below command 17 | - ``sudo apt install wkhtmltopdf`` - for Ubuntu 18 | - ``brew install Caskroom/cask/wkhtmltopdf`` - For Mac 19 | 20 | 21 | ## Steps to Follow 22 | 23 | 24 | 25 | 26 | 1. Create a sample HTML file inside of your project. 27 | 28 | 2. Get go package using the below command for parsing the HTMl template. 29 | - ``go get github.com/SebastiaanKlippert/go-wkhtmltopdf`` 30 | 31 | 3. Get another go package for converting this html parse data to pdf using the below command. 32 | - ``go get html/template`` 33 | 34 | 4. Run below command 35 | - ``go run main.go`` 36 | 37 | ###The previous version of golang was deprecated and not supporting the converter library so dependencies are updated with the new updates. 38 | 39 | #Steps by step: 40 | 41 | - Install ``golang (1.16)`` 42 | - Check ``go version`` (Make Sure it is latest = 1.16) 43 | - ``go mod vendor`` 44 | - ``go build`` 45 | 46 | 47 | ##To Run the project: 48 | 49 | - ``go run main.go`` 50 | 51 | ## Update project dependencies 52 | 53 | - ``go get github.com/SebastiaanKlippert/go-wkhtmltopdf`` v1.6.1 54 | - ``go get html/template`` 55 | 56 | 57 | After finish above steps, You can generate pdf file from html. 58 | 59 | **NOTES:** The pdf will be generated in the storage directory. Don't forget to mention your html and pdf path details inside your code. If you want to generate pdf for static data then it is not mandatory to generate another html for clone data. Use that same sample html inside the code. 60 | 61 | 62 | ## LICENSE! 63 | 64 | Golang HTML to PDF Converter is [MIT-licensed](https://github.com/mindinventory/Golang-HTMLTOPDF-Converter/blob/master/LICENSE) 65 | 66 | ## Let us know! 67 | We’d be really happy if you sent us links to your projects where you use our component. Just send an email to sales@mindinventory.com And do let us know if you have any questions or suggestion regarding our work. 68 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | 5.1.x | :white_check_mark: | 11 | | 5.0.x | :x: | 12 | | 4.0.x | :white_check_mark: | 13 | | < 4.0 | :x: | 14 | 15 | ## Reporting a Vulnerability 16 | 17 | Use this section to tell people how to report a vulnerability. 18 | 19 | Tell them where to go, how often they can expect to get an update on a 20 | reported vulnerability, what to expect if the vulnerability is accepted or 21 | declined, etc. 22 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module Golang-HTML-TO-PDF-Converter 2 | 3 | go 1.16 4 | 5 | require github.com/SebastiaanKlippert/go-wkhtmltopdf v1.6.1 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/SebastiaanKlippert/go-wkhtmltopdf v1.6.1 h1:Arp9w1xMGfnYFQH8don5x7b9oPQWQrUcpFquvBCFvlU= 2 | github.com/SebastiaanKlippert/go-wkhtmltopdf v1.6.1/go.mod h1:zRBvVJtIfhaWvQ9lPIkXrtona4qqSmjZ1HfKvq4dQzI= 3 | -------------------------------------------------------------------------------- /html-pdf-new.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mindinventory/Golang-HTML-TO-PDF-Converter/dff1f03cc34bdb7548f8c3b58cf64b67e46c6bbe/html-pdf-new.gif -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | u "Golang-HTML-TO-PDF-Converter/pdfGenerator" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | r := u.NewRequestPdf("") 11 | 12 | //html template path 13 | templatePath := "templates/sample.html" 14 | 15 | //path for download pdf 16 | outputPath := "storage/example.pdf" 17 | 18 | //html template data 19 | templateData := struct { 20 | Title string 21 | Description string 22 | Company string 23 | Contact string 24 | Country string 25 | }{ 26 | Title: "HTML to PDF generator", 27 | Description: "This is the simple HTML to PDF file.", 28 | Company: "Jhon Lewis", 29 | Contact: "Maria Anders", 30 | Country: "Germany", 31 | } 32 | 33 | if err := r.ParseTemplate(templatePath, templateData); err == nil { 34 | 35 | // Generate PDF with custom arguments 36 | args := []string{"no-pdf-compression"} 37 | 38 | // Generate PDF 39 | ok, _ := r.GeneratePDF(outputPath, args) 40 | fmt.Println(ok, "pdf generated successfully") 41 | } else { 42 | fmt.Println(err) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /pdfGenerator/pdf.go: -------------------------------------------------------------------------------- 1 | package pdfGenerator 2 | 3 | import ( 4 | "bytes" 5 | "html/template" 6 | "io/ioutil" 7 | "log" 8 | "os" 9 | "strconv" 10 | "time" 11 | 12 | "github.com/SebastiaanKlippert/go-wkhtmltopdf" 13 | ) 14 | 15 | // pdf requestpdf struct 16 | type RequestPdf struct { 17 | body string 18 | } 19 | 20 | // new request to pdf function 21 | func NewRequestPdf(body string) *RequestPdf { 22 | return &RequestPdf{ 23 | body: body, 24 | } 25 | } 26 | 27 | // parsing template function 28 | func (r *RequestPdf) ParseTemplate(templateFileName string, data interface{}) error { 29 | 30 | t, err := template.ParseFiles(templateFileName) 31 | if err != nil { 32 | return err 33 | } 34 | buf := new(bytes.Buffer) 35 | if err = t.Execute(buf, data); err != nil { 36 | return err 37 | } 38 | r.body = buf.String() 39 | return nil 40 | } 41 | 42 | // generate pdf function 43 | func (r *RequestPdf) GeneratePDF(pdfPath string, args []string) (bool, error) { 44 | t := time.Now().Unix() 45 | // write whole the body 46 | 47 | if _, err := os.Stat("cloneTemplate/"); os.IsNotExist(err) { 48 | errDir := os.Mkdir("cloneTemplate/", 0777) 49 | if errDir != nil { 50 | log.Fatal(errDir) 51 | } 52 | } 53 | err1 := ioutil.WriteFile("cloneTemplate/"+strconv.FormatInt(int64(t), 10)+".html", []byte(r.body), 0644) 54 | if err1 != nil { 55 | panic(err1) 56 | } 57 | 58 | f, err := os.Open("cloneTemplate/" + strconv.FormatInt(int64(t), 10) + ".html") 59 | if f != nil { 60 | defer f.Close() 61 | } 62 | if err != nil { 63 | log.Fatal(err) 64 | } 65 | 66 | pdfg, err := wkhtmltopdf.NewPDFGenerator() 67 | if err != nil { 68 | log.Fatal(err) 69 | } 70 | 71 | // Use arguments to customize PDF generation process 72 | for _, arg := range args { 73 | switch arg { 74 | case "low-quality": 75 | pdfg.LowQuality.Set(true) 76 | case "no-pdf-compression": 77 | pdfg.NoPdfCompression.Set(true) 78 | case "grayscale": 79 | pdfg.Grayscale.Set(true) 80 | // Add other arguments as needed 81 | } 82 | } 83 | 84 | pdfg.AddPage(wkhtmltopdf.NewPageReader(f)) 85 | 86 | pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4) 87 | 88 | pdfg.Dpi.Set(300) 89 | 90 | err = pdfg.Create() 91 | if err != nil { 92 | log.Fatal(err) 93 | } 94 | 95 | err = pdfg.WriteFile(pdfPath) 96 | if err != nil { 97 | log.Fatal(err) 98 | } 99 | 100 | dir, err := os.Getwd() 101 | if err != nil { 102 | panic(err) 103 | } 104 | 105 | defer os.RemoveAll(dir + "/cloneTemplate") 106 | 107 | return true, nil 108 | } 109 | -------------------------------------------------------------------------------- /storage/example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mindinventory/Golang-HTML-TO-PDF-Converter/dff1f03cc34bdb7548f8c3b58cf64b67e46c6bbe/storage/example.pdf -------------------------------------------------------------------------------- /templates/sample.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Example Pdf 6 | 26 | 27 | 28 |

{{ .Title }}

29 |

{{.Description}}

30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 |
CompanyContactCountry
{{.Company}}{{.Contact}}{{.Country}}
46 | 47 | --------------------------------------------------------------------------------