renderer

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 8 Imported by: 0

README

renderer

Golang module for executing url rendering with chromedp

Requirements

chrome browser installed on host

Browser

BrowserContext values:

  • IdleType: Method to detemine render is complete
    • Type: string (valid values: networkIdle, InteractiveTime)
    • Default: networkIdle
  • BrowserExecPath: Manually set chrome / chromium browser's executable path
    • Type: String
    • Default: Empty string (Auto detect)
  • NoSandbox: Set to disable using sandbox for isolating browser process (usually use in docker container or AWS lambda)
    • Type: bool
    • Default: false
  • SingleProcess: Set to force the browser to run in single process, rather than using separate processes (tested required to work for headless chromium in aws lambda function)
    • Type: bool
    • Default: false
  • DebugMode: Output debug message if set to true
    • Type: bool
    • Default: false

Renderer

RendererContext values:

  • Headless: Browser execution mode
    • Type: bool
    • Default: false
  • WindowWidth: Width of browser's window size
    • Type: Int
    • Default: 1000
  • WindowHeight: Height of browser's window size
    • Type: Int
    • Default: 1000
  • Timeout: Seconds before rendering timeout
    • Type: Int
    • Default: 30
  • ImageLoad: Load image when rendering
    • Type: bool
    • Default: false
  • SkipFrameCount: Skip first n framces with same id as init frame, only valid with idleType=networkIdle (Use on page with protection like CloudFlare)
    • Type: Int
    • Default: 0
Example

See usage example at examples

Build Example / Test
go build -o render.out
Run Example / Test
Usage of ./render:
  -bHeight int
        height of browser window's size (default 1080)
  -bWidth int
        width of browser window's size (default 1920)
  -browserPath string
        manually set browser executable path
  -debug
        turn on for outputing debug message
  -headless
        automation browser execution mode (default true)
  -idleType string
        how to determine loading idle and return, valid input: networkIdle, InteractiveTime (default "networkIdle")
  -imageLoad
        indicate if load image when rendering
  -lambda
        indicate if running on AWS Lambda environment
  -sandbox
        indicate if using sandbox for isolating browser process (default true)
  -singleProcess
        indicate if using single process for browser (force headless mode to be true)
  -skipFrameCount int
        skip first n frames with same id as init frame, only valid with idleType=networkIdle
  -timeout int
        seconds before timeout when rendering (default 30)

Render PDF

PdfContext values:

  • Landscape: Set paper orientation to landscape
    • Type: bool
    • Default: false
  • DisplayHeaderFooter: Display header and footer
    • Type: bool
    • Default: false
  • PaperWidthCm: Paper width in centimeter
    • Type: float64
    • Default: 21
  • PaperHeightCm: Paper height in centimeter
    • Type: float64
    • Default: 29.7
  • MarginTopCm: Top margin in centimeter
    • Type: float64
    • Default: 1
  • MarginBottomCm: Bottom margin in centimeter
    • Type: float64
    • Default: 1
  • MarginLeftCm: Left margin in centimeter
    • Type: float64
    • Default: 1
  • MarginRigthCm: Right margin in centimeter
    • Type: float64
    • Default: 1
Example

See usage example at examples

Build Example / Test
go build -o pdf.out
Run Example / Test
Usage of ./pdf:
  -browserPath string
        manually set browser executable path
  -debug
        turn on for outputing debug message
  -headerFooter
        show header and footer
  -idleType string
        how to determine loading idle and return, valid input: networkIdle, InteractiveTime (default "networkIdle")
  -lambda
        indicate if running on AWS Lambda environment
  -landscape
        create pdf in landscape layout
  -marginBottom float
        bottom margin in centimeter (default 1)
  -marginLeft float
        left margin in centimeter (default 1)
  -marginRight float
        right margin in centimeter (default 1)
  -marginTop float
        top margin in centimeter (default 1)
  -paperHeight float
        paper height in centimeter
  -paperWidth float
        paper width in centimeter
  -sandbox
        indicate if using sandbox for isolating browser process (default true)
  -singleProcess
        indicate if using single process for browser (force headless mode to be true)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBrowserContextNotFound  = errors.New("browser context not found")
	ErrRendererContextNotFound = errors.New("renderer context not found")
	ErrPdfContextNotFound      = errors.New("pdf context not found")
)

Functions

func RenderPage

func RenderPage(ctx context.Context, urlStr string) ([]byte, error)

RenderPage rendered given url in browser and returns result html content

func RenderPdf added in v0.5.0

func RenderPdf(ctx context.Context, urlStr string) ([]byte, error)

RenderPdf turns given url page into pdf and return result

func WithBrowserContext added in v0.6.0

func WithBrowserContext(ctx context.Context, bc *BrowserContext) context.Context

WithBrowserContext add BrowserContext with browserKey to context and return new context value

func WithPdfContext added in v0.5.0

func WithPdfContext(ctx context.Context, pc *PdfContext) context.Context

WithPdfContext add PdfContext with pdfKey to context and return new context value

func WithRendererContext added in v0.4.0

func WithRendererContext(ctx context.Context, rc *RendererContext) context.Context

WithRendererContext add RendererContext with rendererKey to context and return new context value

Types

type BrowserContext added in v0.6.0

type BrowserContext struct {
	IdleType        string
	BrowserExecPath string
	NoSandbox       bool
	DebugMode       bool
	SingleProcess   bool
}

BrowserContext is use for general browser settings

func GetBrowserContext added in v0.6.0

func GetBrowserContext(ctx context.Context) (*BrowserContext, error)

GetBrowserContext read and return BrowserContext from input context ErrBrowserContextNotFound is returned if browserKey not exist

type PdfContext added in v0.5.0

type PdfContext struct {
	Landscape           bool
	DisplayHeaderFooter bool
	PaperWidthCm        float64
	PaperHeightCm       float64
	MarginTopCm         float64
	MarginBottomCm      float64
	MarginLeftCm        float64
	MarginRightCm       float64
}

PdfContext is use for print PDF settings

func GetPdfContext added in v0.5.0

func GetPdfContext(ctx context.Context) (*PdfContext, error)

GetPdfContext read and return PdfContext from input context ErrPdfContextNotFound is returned if pdfKey not exist

type RendererContext added in v0.4.0

type RendererContext struct {
	Headless       bool
	WindowWidth    int
	WindowHeight   int
	Timeout        int
	ImageLoad      bool
	SkipFrameCount int
}

RendererContext is use for renderer settings

func GetRendererContext added in v0.4.0

func GetRendererContext(ctx context.Context) (*RendererContext, error)

GetRendererContext read and return RendererContext from input context ErrRendererContextNotFound is returned if rendererKey not exist

Directories

Path Synopsis
examples
pdf

Jump to

Keyboard shortcuts

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