Open multiple .png files and save them in one pdf file

Hello rooters,
I have many saved .png images and I want to save them all in one pdf file.

  void plot(){
  TCanvas *c = new TCanvas("c","c",800,600);
  c->Print("xs_c.pdf(","pdf");

 
      for(Int_t k =0;k<3;k++){
	TImage *im = TImage::Open(Form("im_%d.png",k));
        im->Draw();     
        c->Print("xs_c.pdf","pdf");
        im->Draw();
        }

    
  c->Print("xs_c.pdf)","pdf");
}


However, I keep getting the error:
Warning in TASImage::Paint: PDF not implemeted yet
and the pdf file is empty.
I tried ps , but the file is so big and doesnt have good resolution.
png, jpg only saves one page.

Any suggestions?

Thanks

Sharey
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


just in case no C++/ROOT-er bites, here is a simple program in Go:

package main

import (
	"flag"
	"log"

	"github.com/jung-kurt/gofpdf"
)

func main() {
	var (
		x    = flag.Float64("width", 800, "page width")
		y    = flag.Float64("height", 600, "page height")
		unit = flag.String("unit", "pt", "units for page dimension")
	)

	flag.Parse()

	names := flag.Args()

	cfg := gofpdf.InitType{
		UnitStr: *unit,
		Size: gofpdf.SizeType{
			Wd: *x,
			Ht: *y,
		},
	}
	pdf := gofpdf.NewCustom(&cfg)
	for _, name := range names {
		image(pdf, name)
	}

	err := pdf.OutputFileAndClose("output.pdf")
	if err != nil {
		log.Fatalf("could not save PDF file: %v", err)
	}
}

func image(pdf *gofpdf.Fpdf, name string) {
	pdf.AddPage()
	pdf.ImageOptions(name, 0, 0, 0, 0, false, gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true}, 0, "")
}

You can do it without root using convert, which should be available if you have imagemagick:
convert InputFileNames output.pdf
where InputFileNames are the file names separated by space (e.g. file1.png file2.png file3.png). There may be a maximum char size for the list, so if you have too many files you could join in batches to temporary pdf files and then join these pdf files into a final one.

You can also try to use a PS file as output instead of a PDF.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.