add new functions for zip and byte slices
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package converter
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -63,7 +65,7 @@ func (c *Converter) SetProgressCallback(cb func(progress int)) {
|
||||
}
|
||||
|
||||
// Convert converts all given input files
|
||||
func (c *Converter) Convert(files ...models.File) error {
|
||||
func (c *Converter) ConvertToPdf(files ...models.File) error {
|
||||
for i, f := range files {
|
||||
if c.progress != nil {
|
||||
c.progress(i + 1)
|
||||
@@ -127,7 +129,7 @@ func (c *Converter) Convert(files ...models.File) error {
|
||||
}
|
||||
|
||||
// Convert converts all given input files
|
||||
func (c *Converter) ConvertHtmls(htmls ...models.Html) error {
|
||||
func (c *Converter) ConvertHtmlsToPdf(htmls ...models.Html) error {
|
||||
for i, h := range htmls {
|
||||
if c.progress != nil {
|
||||
c.progress(i + 1)
|
||||
@@ -183,6 +185,126 @@ func (c *Converter) ConvertHtmls(htmls ...models.Html) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert converts all given input files
|
||||
func (c *Converter) ConvertHtmlsToBytes(htmls ...models.Html) ([][]byte, error) {
|
||||
var output [][]byte
|
||||
for i, h := range htmls {
|
||||
if c.progress != nil {
|
||||
c.progress(i + 1)
|
||||
}
|
||||
if len(h.Html) == 0 {
|
||||
return nil, fmt.Errorf("no .html input provided")
|
||||
} else if h.Output == "" || filepath.Ext(h.Output) != ".pdf" {
|
||||
return nil, fmt.Errorf("no .pdf output file path provided: %s", h.Output)
|
||||
}
|
||||
|
||||
taskCtx, taskCancel := chromedp.NewContext(c.browserCtx)
|
||||
timeoutCtx, timeoutCancel := context.WithTimeout(taskCtx, 60*time.Second)
|
||||
|
||||
var pdfData []byte
|
||||
err := chromedp.Run(timeoutCtx,
|
||||
// Start with a blank page
|
||||
chromedp.Navigate("about:blank"),
|
||||
// Inject HTML directly
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
frameTree, err := page.GetFrameTree().Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return page.SetDocumentContent(frameTree.Frame.ID, string(h.Html)).Do(ctx)
|
||||
}),
|
||||
chromedp.WaitReady("body", chromedp.ByQuery),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
buf, _, err := page.PrintToPDF().
|
||||
WithPrintBackground(true).
|
||||
WithPaperWidth(8.27).
|
||||
WithPaperHeight(11.69).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pdfData = buf
|
||||
return nil
|
||||
}),
|
||||
)
|
||||
|
||||
timeoutCancel()
|
||||
taskCancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add to array of bytes
|
||||
output = append(output, pdfData)
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// Convert html to pdf and return zip as bytes
|
||||
func (c *Converter) ConvertHtmlsToZip(htmls ...models.Html) ([]byte, error) {
|
||||
zipBuf := new(bytes.Buffer)
|
||||
zipWriter := zip.NewWriter(zipBuf)
|
||||
|
||||
for i, h := range htmls {
|
||||
if c.progress != nil {
|
||||
c.progress(i + 1)
|
||||
}
|
||||
if len(h.Html) == 0 {
|
||||
return nil, fmt.Errorf("no .html input provided")
|
||||
} else if h.Output == "" || filepath.Ext(h.Output) != ".pdf" {
|
||||
return nil, fmt.Errorf("no .pdf output file path provided: %s", h.Output)
|
||||
}
|
||||
|
||||
taskCtx, taskCancel := chromedp.NewContext(c.browserCtx)
|
||||
timeoutCtx, timeoutCancel := context.WithTimeout(taskCtx, 60*time.Second)
|
||||
|
||||
var pdfData []byte
|
||||
err := chromedp.Run(timeoutCtx,
|
||||
// Start with a blank page
|
||||
chromedp.Navigate("about:blank"),
|
||||
// Inject HTML directly
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
frameTree, err := page.GetFrameTree().Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return page.SetDocumentContent(frameTree.Frame.ID, string(h.Html)).Do(ctx)
|
||||
}),
|
||||
chromedp.WaitReady("body", chromedp.ByQuery),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
buf, _, err := page.PrintToPDF().
|
||||
WithPrintBackground(true).
|
||||
WithPaperWidth(8.27).
|
||||
WithPaperHeight(11.69).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pdfData = buf
|
||||
return nil
|
||||
}),
|
||||
)
|
||||
|
||||
timeoutCancel()
|
||||
taskCancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add to zip
|
||||
f, err := zipWriter.Create(h.Output)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Write(pdfData)
|
||||
}
|
||||
zipWriter.Close()
|
||||
|
||||
return zipBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *Converter) Close() {
|
||||
// Close browser first, then allocator
|
||||
if c.browserCancel != nil {
|
||||
|
||||
Reference in New Issue
Block a user