GithubHelp home page GithubHelp logo

grahms / html2pdf Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 5 KB

HTML 2 PDF is a Go library that provides an interface for converting HTML content to PDF using Chromium. It interacts with a headless Chromium instance and generate PDFs from HTML content.

Go 100.00%

html2pdf's Introduction

html2pdf

html2pdf is a Go library that provides an interface for converting HTML content to PDF using Chromium. It interacts with a headless Chromium instance and generate PDFs from HTML content.

Installation

To install the html2pdf package, use go get:

go get github.com/grahms/html2pdf

Prerequisites

Ensure that Chromium or Google Chrome is installed on your machine. You can download it from here or install it using a package manager:

  • On macOS:
    brew install chromium
  • On Ubuntu/Debian:
    sudo apt-get install chromium-browser

Usage

Here's a basic example of how to use the html2pdf library to convert HTML content to a PDF file:

Example

package main

import (
	"fmt"
	"github.com/grahms/html2pdf"
	"log"
	"os"
)

func main() {
	html := `<html>
<body>
<div>text</div>
<img src="https://pkg.go.dev/static/shared/gopher/package-search-700x300.jpeg"/>
<img src="https://go.dev/images/gophers/motorcycle.svg"/>
<img src="https://go.dev/images/go_google_case_study_carousel.png" />
</body>
</html>`

	// Create a new PDFGenerator instance
	pdfGen, err := html2pdf.New()
	if err != nil {
		log.Fatalf("Failed to initialize PDF generator: %v", err)
	}
	defer pdfGen.Close()

	// Convert HTML to PDF
	pdfBuffer, err := pdfGen.GeneratePDF(&html)
	if err != nil {
		log.Fatalf("Failed to convert HTML to PDF: %v", err)
	}

	// Save the PDF buffer to a file
	if err := saveToFile("output.pdf", pdfBuffer); err != nil {
		log.Fatalf("Failed to save PDF to file: %v", err)
	}

	fmt.Println("PDF successfully created: output.pdf")
}

// saveToFile writes the PDF buffer to a file.
func saveToFile(fileName string, data []byte) error {
	return os.WriteFile(fileName, data, 0644)
}

Features

  • Easy to use: Simple API to generate PDFs from HTML content.
  • Graceful shutdown: Handles interrupt signals to ensure the Chromium instance is properly terminated.
  • Mockable: Provides an interface for easy testing and mocking.

API

PDFGeneratorInterface

type PDFGeneratorInterface interface {
	GeneratePDF(html *string) ([]byte, error)
	Close()
}

PDFGenerator

New() (*PDFGenerator, error)

Creates a new PDFGenerator and initializes the browser context.

GeneratePDF(html *string) ([]byte, error)

Converts HTML content to a PDF buffer.

Close()

Shuts down the browser context.

Handling Interrupt Signals

The library automatically sets up a signal handler to gracefully shut down the PDFGenerator on interrupt signals.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.

html2pdf's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.