html2pdf

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 9 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

html2pdf

html2pdf is a Go package designed specifically for converting HTML content to PDF documents. It wraps the functionality of Rod for browser automation and pdfcpu for PDF generation, providing a simple and efficient way to create PDFs from HTML content.

  • this is very simple code, basically a wrapper, for my personal usage as i struggled to find easy go library to suit my needs for converting html to pdf

  • it is not a perfect solution and not yet completed and probably have bugs :)

Installation

go get github.com/MhmoudGit/html2pdf

Usage

package main

import (
	"fmt"
	"html/template"
	"github.com/MhmoudGit/html2pdf"
)

type Todo struct {
	ID   int
	Item string
}

var tmpl = `
<!DOCTYPE html>
<html>
	<head>
		<title>Todo Information</title>
	</head>
	<body>
			<h1 style="color: red">{{.ID}}</h1>
			<p style="color: green; font-weight: bold">{{.Item}}.</p>
	</body>
</html>
`

func main() {
    // directory for saving generated data
	tempDir := "files"
    // the final output
	mergedPdf := "example.pdf"

	todos := []Todo{
		{ID: 1, Item: "First item"},
	}

	template, err := template.New("todo").Parse(tmpl)
	if err != nil {
		fmt.Println(err)
	}

    // create a pdf generator
	g := html2pdf.Generator[Todo]{
		OutputPath: tempDir,
		FinalPdf:   mergedPdf,
		Template:   template,
		Data:       todos,
	}

    // generate pdf
	err = g.CreatePdf()
	if err != nil {
		fmt.Println(err)
	}

    // (optional)
    // delete the generated templates and pdf
	err = g.DeleteFiles()
	if err != nil {
		fmt.Println(err)
	}
}

  • Find example using html file and css styling in the example folder

Acknowledgments

  • Rod - A powerful Go package for browser automation.
  • PDFCPU - A PDF processing library written in Go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator[T any] struct {
	OutputPath string             // directory path for generated data
	FinalPdf   string             // the merged pdf name (make sure to include .pdf in the name)
	Template   *template.Template // html template
	Data       []T                // valid data for feeding the template

	HtmlFiles      []*os.File // list of generated html files
	PdfFiles       []string   // list of generated pdf files
	SingleHtmlFile bool       // If you want the template to be single file only
	// contains filtered or unexported fields
}

func (*Generator[T]) CapturePDF

func (g *Generator[T]) CapturePDF(browser *rod.Browser, htmlUrl, outputPath string) error

Automate opening a prowser then capture the html page as single pdf file

func (*Generator[T]) CreateHtmlFile

func (g *Generator[T]) CreateHtmlFile(id int) (*os.File, error)

func (*Generator[T]) CreatePdf

func (g *Generator[T]) CreatePdf() error

Generate pdf file from multible html templates

func (*Generator[T]) DeleteFiles

func (g *Generator[T]) DeleteFiles() error

Delete html and pdf files except the merged pdf

func (*Generator[T]) GenerateTemplates

func (g *Generator[T]) GenerateTemplates() error

Generate html templates from the given data and save them into .g.OutputPath

func (*Generator[T]) MergePDFs

func (g *Generator[T]) MergePDFs(inputFiles []string, outputFile string) error

Merging all generated pdf together and create the output file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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