orcgen

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 2 Imported by: 0

README

Orcgen

Orcgen is a Go package that enables the conversion of web pages and HTML content to various file formats like PDF, PNG, and JPEG. It provides an easy-to-use interface for generating these file formats from web pages and HTML.

Functionalities

Orcgen provides the following functionalities:

  • Conversion of HTML content to PDF, PNG, and JPEG files.
  • Conversion of web pages to PDF, PNG, and JPEG files.
  • The package provides functions to convert HTML content and web pages to the desired file format.
  • The ability to configure the conversion process by setting parameters like load timeout and page idle time.
  • The package supports the creation of full-page screenshots of web pages.

Installation

To use Orcgen, you can install it via Go modules:

    go get github.com/luabagg/orcgen

Then you can import it in your Go code:

    import "github.com/luabagg/orcgen"

Example Usage

The package comes with examples that demonstrate the usage of the various functions and features provided by Orcgen.

You can see it in examples_test.go page.

Contributors

This project is an open-source project, and contributions from other developers are welcome. If you encounter any issues or have suggestions for improvement, please submit them on the project's GitHub page.

Documentation

Overview

Package orcgen generates files from HTML - any static webpage can be informed, or even an HTML file. The file will be generated according the choosen extension.

Index

Examples

Constants

View Source
const (
	// PDF const.
	PDF = internal.PDF
	// PNG const.
	PNG = internal.PNG
	// JPEG const.
	JPEG = internal.JPEG
)

Valid extension types constants.

Variables

This section is empty.

Functions

func ConvertHTML

func ConvertHTML(ext internal.Ext, html []byte, output string) error

ConvertHTML converts the informed bytes to the ext format, and saves the file.

ext is the extension to be converted to (use the defined constants above). html is the html byte array (if it's a filepath, use os.ReadFile(filepath)). output is a filepath containing the extension.

The connection is automatically closed.

Example

ExampleConvertHTML gives examples using the ConvertHTML function from

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"

	"github.com/luabagg/orcgen"
)

func main() {
	filename := "html.pdf"
	err := orcgen.ConvertHTML(orcgen.PDF, getHTML(), getName(filename))
	if err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

}

func getHTML() []byte {
	file := filepath.Join(getBasepath(), "testdata/test.html")
	html, _ := os.ReadFile(file)

	return html
}

func getName(name string) string {
	return filepath.Join(getBasepath(), "testdata", name)
}

func getBasepath() string {
	_, b, _, _ := runtime.Caller(0)
	return filepath.Dir(b)
}
Output:

html.pdf generated succesfully

func ConvertWebpage

func ConvertWebpage(ext internal.Ext, url string, output string) error

ConvertWebpage converts the url to the ext format, and saves the file.

ext is the extension to be converted to (use the defined constants above). url is the url to convert. output is a filepath containing the extension.

The connection is automatically closed.

Example

ExampleConvertWebpage gives examples using the ConvertWebpage function from

package main

import (
	"fmt"
	"path/filepath"
	"runtime"

	"github.com/luabagg/orcgen"
)

func main() {
	filename := "github.pdf"
	err := orcgen.ConvertWebpage(orcgen.PDF, "https://www.github.com", getName(filename))
	if err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

}

func getName(name string) string {
	return filepath.Join(getBasepath(), "testdata", name)
}

func getBasepath() string {
	_, b, _, _ := runtime.Caller(0)
	return filepath.Dir(b)
}
Output:

github.pdf generated succesfully

func New

func New(ext internal.Ext) *director.Director

New starts a new Director - the Director contains the available methods for file conversion.

ext is the extension to be converted to (use the defined constants above).

Connect and Close are used for the Browser connection control. ConvertWebpage and ConvertHTML are used for page conversion.

There are a set of setters for specific config.

Example

ExampleNew gives examples using the New function from orcgen.

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"time"

	"github.com/luabagg/orcgen"
)

func main() {
	// starts the connection.
	gen := orcgen.New(orcgen.PDF)
	defer gen.Close()

	/* using for HTML conversion */

	// this generates a pdf file with the HTML content.
	f, _ := gen.ConvertHTML(getHTML())

	filename := "html.pdf"
	if err := f.Output(getName(filename)); err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

	// this generates a png file with the HTML content.
	// notice the SetFullPage use here.
	f, _ = gen.SetExt(orcgen.PNG).
		SetFullPage(true).
		ConvertHTML(getHTML())

	filename = "html.png"
	if err := f.Output(getName(filename)); err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

	/* using for URL conversion */

	// this generates a pdf file from www.google.com.
	f, _ = gen.SetExt(orcgen.PDF).
		ConvertWebpage("https://www.google.com")

	filename = "google.pdf"
	if err := f.Output(getName(filename)); err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

	// this generates a jpeg file from www.twitter.com.
	// full config example.
	f, _ = gen.SetExt(orcgen.JPEG).
		SetFullPage(true).
		SetLoadTimeout(5 * time.Second).
		SetPageIdleTime(3 * time.Second).
		ConvertWebpage("https://www.twitter.com")

	filename = "twitter.jpeg"
	if err := f.Output(getName(filename)); err == nil {
		fmt.Printf("%s generated succesfully\n", filename)
	}

}

func getHTML() []byte {
	file := filepath.Join(getBasepath(), "testdata/test.html")
	html, _ := os.ReadFile(file)

	return html
}

func getName(name string) string {
	return filepath.Join(getBasepath(), "testdata", name)
}

func getBasepath() string {
	_, b, _, _ := runtime.Caller(0)
	return filepath.Dir(b)
}
Output:

html.pdf generated succesfully
html.png generated succesfully
google.pdf generated succesfully
twitter.jpeg generated succesfully

Types

This section is empty.

Directories

Path Synopsis
Package internal contains the builder implementation.
Package internal contains the builder implementation.
generator
Package generator contains builders used for page conversion (example HTML to PDF).
Package generator contains builders used for page conversion (example HTML to PDF).
generator/jpeg
Package jpeg implements the builder for JPEG files.
Package jpeg implements the builder for JPEG files.
generator/pdf
Package pdf implements the builder for PDF files.
Package pdf implements the builder for PDF files.
generator/png
Package png implements the builder for PNG files.
Package png implements the builder for PNG files.
rod
Package rod contains go-rod implementation.
Package rod contains go-rod implementation.
pkg
director
Package director provides functionality for generating files from HTML using the Rod package.
Package director provides functionality for generating files from HTML using the Rod package.
fileinfo
Package fileinfo is used for file information control.
Package fileinfo is used for file information control.

Jump to

Keyboard shortcuts

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