PDF to windows printer


(Ronald Müller) #1

Hi together

I create a simple pdf with some simple formats with gofpdf and would print this pdf with a labelprinter on a windows machine. it works with simple text, but not with pdf. has someone an example for me? that would be great.

i pass the output as a byte slice from gofpdf to this function. but i get printer errors:

var buf bytes.Buffer
pdf.Output(&buf)
print2(buf.Bytes())

func print2(content []byte) {
	fmt.Println(string(content))
	name, err := prt.Default() // returns name of Default Printer as string
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(name)
	p, err := prt.Open(name) // Opens the named printer and returns a *Printer
	if err != nil {
		fmt.Println(err)
	}

	//err = p.StartDocument("test", "text") // test: doc name, text: doc type
	err = p.StartDocument("test", "RAW")
	if err != nil {
		fmt.Println(err)
	}
	err = p.StartPage() // begin a new page
	if err != nil {
		fmt.Println(err)
	}
	//n, err := p.Write([]byte("Hello, Printer!")) // Send some text to the printer
	n, err := p.Write(content) // Send some text to the printer
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Num of bytes written to printer:", n)
	err = p.EndPage() // end of page
	if err != nil {
		fmt.Println(err)
	}
	err = p.EndDocument() // end of document
	if err != nil {
		fmt.Println(err)
	}
	err = p.Close() // close the resource
	if err != nil {
		fmt.Println(err)
	}
}

Or is it possible to send the file generated with gofpdf to the printer directly? I have no idea anymore…

Thanks for your help!


(catacombs) #2

What is the error?


(Ronald Müller) #3

I don‘t know the exact error message on the printer. the pdf is sent correctly without any error message out of the gocode. if i save the pdf to the disc and open it with a pdf reader it looks totally ok. the error should be somewhere in the format the message is sendt to the printer. you can test it, if you take each other pdf on your disc an send it through the code to your printer.


(Sean Killian) #5

Hi, @Dino,

The sample you included doesn’t include enough to compile the source to test it myself. Do you have the full code somewhere on Github or something?