Images

Images

The image formats PNG, JPEG and PDF are supported. You can load an image with

func (pw *PDF) LoadImageFile(filename string) (*Imagefile, error)

which returns an image object:

type Imagefile struct {
	Format        string
	NumberOfPages int
	PageSizes     map[int]map[string]map[string]float64
	Filename      string
	ScaleX        float64
	ScaleY        float64
	W             int
	H             int
	Box           string
	PageNumber    int
}

There is not much more to say about images. You can get an internal name with

func (imgf *Imagefile) InternalName() string

which can then be used in a Page content stream. Only images that are part of pages are written to the resulting PDF file.

To add an image to a page, append it to the page’s Images slice:

img, err := pw.LoadImageFile("ocean.pdf")
if err != nil {
    // error handling
}

stream := pw.NewObject()
stream.Data.WriteString(fmt.Sprintf("q %s Do Q\n", img.InternalName()))

page1 := pw.AddPage(stream, 0)
page1.Images = append(page1.Images, img)

The image gets written to the PDF file only once, even if it included multiple times.

You can see an example at github.com/boxesandglue/boxesandglue-examples/tree/main/baseline/image.