Extend configuration

This commit is contained in:
2023-03-28 21:07:42 +03:00
parent 6839a264db
commit 8d2af4ad65
7 changed files with 63 additions and 24 deletions

View File

@@ -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
}

View File

@@ -8,6 +8,7 @@ import (
"net/http/cookiejar"
"time"
"github.com/derfenix/webarchive/config"
"github.com/derfenix/webarchive/entity"
)
@@ -15,7 +16,7 @@ type processor interface {
Process(ctx context.Context, url string) ([]entity.File, error)
}
func NewProcessors() (*Processors, error) {
func NewProcessors(cfg config.Config) (*Processors, error) {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: nil,
})
@@ -53,7 +54,7 @@ func NewProcessors() (*Processors, error) {
procs := Processors{
processors: map[entity.Format]processor{
entity.FormatHeaders: NewHeaders(httpClient),
entity.FormatPDF: NewPDF(),
entity.FormatPDF: NewPDF(cfg.PDF),
},
}