mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 21:35:34 +03:00
Extend configuration
This commit is contained in:
@@ -7,37 +7,46 @@ import (
|
||||
|
||||
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
|
||||
|
||||
"github.com/derfenix/webarchive/config"
|
||||
"github.com/derfenix/webarchive/entity"
|
||||
)
|
||||
|
||||
func NewPDF() *PDF {
|
||||
return &PDF{}
|
||||
func NewPDF(cfg config.PDF) *PDF {
|
||||
return &PDF{cfg: cfg}
|
||||
}
|
||||
|
||||
type PDF struct{}
|
||||
type PDF struct {
|
||||
cfg config.PDF
|
||||
}
|
||||
|
||||
func (P *PDF) Process(_ context.Context, url string) ([]entity.File, error) {
|
||||
func (p *PDF) Process(_ context.Context, url string) ([]entity.File, error) {
|
||||
gen, err := wkhtmltopdf.NewPDFGenerator()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new pdf generator: %w", err)
|
||||
}
|
||||
|
||||
gen.Dpi.Set(300)
|
||||
gen.Dpi.Set(p.cfg.DPI)
|
||||
gen.PageSize.Set(wkhtmltopdf.PageSizeA4)
|
||||
gen.Orientation.Set(wkhtmltopdf.OrientationPortrait)
|
||||
gen.Grayscale.Set(false)
|
||||
|
||||
if p.cfg.Landscape {
|
||||
gen.Orientation.Set(wkhtmltopdf.OrientationLandscape)
|
||||
} else {
|
||||
gen.Orientation.Set(wkhtmltopdf.OrientationPortrait)
|
||||
}
|
||||
|
||||
gen.Grayscale.Set(p.cfg.Grayscale)
|
||||
gen.Title.Set(url)
|
||||
|
||||
page := wkhtmltopdf.NewPage(url)
|
||||
page.PrintMediaType.Set(true)
|
||||
page.PrintMediaType.Set(p.cfg.MediaPrint)
|
||||
page.JavascriptDelay.Set(200)
|
||||
page.LoadMediaErrorHandling.Set("ignore")
|
||||
page.FooterRight.Set("[page]")
|
||||
page.HeaderLeft.Set(url)
|
||||
page.HeaderRight.Set(time.Now().Format(time.DateOnly))
|
||||
page.FooterFontSize.Set(10)
|
||||
page.Zoom.Set(1)
|
||||
page.ViewportSize.Set("1920x1080")
|
||||
page.Zoom.Set(p.cfg.Zoom)
|
||||
page.ViewportSize.Set(p.cfg.Viewport)
|
||||
|
||||
gen.AddPage(page)
|
||||
|
||||
@@ -46,7 +55,7 @@ func (P *PDF) Process(_ context.Context, url string) ([]entity.File, error) {
|
||||
return nil, fmt.Errorf("create pdf: %w", err)
|
||||
}
|
||||
|
||||
file := entity.NewFile("page.pdf", gen.Bytes())
|
||||
file := entity.NewFile(p.cfg.Filename, gen.Bytes())
|
||||
|
||||
return []entity.File{file}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user