htmlpdfgg

package module
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

HTMLPDFgg

ease of converting html to pdf

Get Started
Basic usage

hpg.Generate() will write to a buffer pdfBuffer

pdfBuffer, err := hpg.Generate()
if err != nil {
 panic(err)
}


 
 

write to file from buffer, see on example/write_to_file.go

templateString := `
    <html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ header }}</h1>
        <p>{{ content }}</p>
		<table border="1">
            <tr>
                <th>Name</th>
                <th>Age</th>
            </tr>
            {% for order in orders %}
            <tr>
                <td>{{ order.id }}</td>
                <td>
				 {% for item in order.items %}
				 	{{ item.name }}<br/>
				 {% endfor %}
				 </td>
            </tr>
            {% endfor %}
        </table>
    </body>
    </html>
    `

	orderItems := []map[string]interface{}{
		{"id": "123", "name": "item1"},
		{"id": "124", "name": "item2"},
		{"id": "125", "name": "item3"},
	}
	orders := []map[string]interface{}{
		{"id": "123", "items": orderItems},
		{"id": "124", "items": orderItems},
		{"id": "125", "items": orderItems},
	}

	data := map[string]interface{}{
		"title":   "htmlpdf",
		"header":  "Generate from htmlpdf",
		"content": "This is a sample PDF created with Pongo2 and chromedp.",
		"orders":  orders,
	}

	hpg := htmlpdfgg.HtmlPDF{}
	hpg.SetTemplateData(templateString, data)
	pdfBuffer, err := hpg.Generate()
	if err != nil {
		panic(err)
	}

	if err := os.WriteFile("output.pdf", pdfBuffer, 0644); err != nil {
		panic(err)
	}
TODO:
  • supported with watermark

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorTemplateEmpty = errors.New("template cant empty")
)

Functions

This section is empty.

Types

type HtmlPDF

type HtmlPDF struct {
	// contains filtered or unexported fields
}

func (*HtmlPDF) Generate

func (h *HtmlPDF) Generate() ([]byte, error)

func (*HtmlPDF) SetTemplateData

func (h *HtmlPDF) SetTemplateData(templateStr string, data map[string]interface{}) *HtmlPDF

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址