Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image not showing up in header #1455

Open
repli2dev opened this issue Mar 25, 2024 · 0 comments
Open

Image not showing up in header #1455

repli2dev opened this issue Mar 25, 2024 · 0 comments

Comments

@repli2dev
Copy link

What versions are you running?

$ go list -m github.com/chromedp/chromedp
github.com/chromedp/chromedp v0.9.5
$ google-chrome --version
123.0.6312.59
$ go version
go version go1.22.1 darwin/arm64

What did you do? Include clear steps.

  1. Run below code
  2. Check the output
package main

import (
	"context"
	"github.com/chromedp/cdproto/page"
	"github.com/chromedp/chromedp"
	"log"
	"os"
	"time"
)

func main() {

	// https://github.com/chromedp/chromedp/issues/904
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.Flag("disable-gpu", true),
	)
	allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	allocCtx, _ = context.WithTimeout(allocCtx, 20*time.Second)
	taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithDebugf(log.Printf))
	defer cancel()

	var printToPdfParameters = &page.PrintToPDFParams{
		MarginRight:  0,
		MarginLeft:   0,
		MarginBottom: 0,
		MarginTop:    0,
		Landscape:    false,
	}

	printToPdfParameters.PrintBackground = true

	printToPdfParameters.HeaderTemplate = "<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/48px-Google_Chrome_icon_%28February_2022%29.svg.png\" height=\"48\" width=\"48\" />"
	printToPdfParameters.DisplayHeaderFooter = true

	requestBody := "<br><br>Content<br><br><br><br><br><br><img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/48px-Google_Chrome_icon_%28February_2022%29.svg.png\" height=\"48\" width=\"48\" />"

	tmpDir := "/tmp"
	tmpFile, err := os.CreateTemp(tmpDir, "tmp-html-file-*.html")
	if err != nil {
		panic(err)
	}

	defer func() {
		err = tmpFile.Close()
		if err != nil {
			panic(err)
		}
	}()

	err = os.WriteFile(tmpFile.Name(), []byte(requestBody), 0600) //nolint:all

	if err != nil {
		panic(err)
	}

	type Result struct {
		Error error  `json:"error"`
		Data  []byte `json:"data"`
	}

	resultChan := make(chan *Result, 1)

	tasks := chromedp.Tasks{
		chromedp.Navigate("file://" + tmpFile.Name()),
		chromedp.ActionFunc(func(ctx context.Context) error {
			pdfContent, _, err := printToPdfParameters.Do(ctx)
			resultChan <- &Result{
				Error: err,
				Data:  pdfContent,
			}
			return err
		}),
	}

	err = chromedp.Run(taskCtx, tasks)

	if err != nil {
		panic(err)
	}

	//fmt.Println((<-resultChan).Data)
	data := (<-resultChan).Data
	// store result into PDF in temp location
	err = os.WriteFile(tmpFile.Name()+".pdf", []byte(data), 0600) //nolint:all
	log.Println(tmpFile.Name() + ".pdf")
}

What did you expect to see?

Logo visible in header too.

What did you see instead?

Snímek obrazovky 2024-03-25 v 13 41 18

Other notes

If I use base64 image embedding than the image shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant