The below code should print some formatted text on a labeled printer in the network.
I have created a PDF which I would send to the printer but I get only errors- "wrong medium..."
Note: The printer printed fine with standard font/standard size text, but I should be able to print formatted text as well.
Simple text is working on a Brother QL-500. But I get only errors on the printer and not in the code if i send the pdf content. Raw settings don't work either.
Does someone have a solution and example code?
Libraries:
github.com/jung-kurt/gofpdfgithub.com/alexbrainman/printer
Sample Code:
package main
import (
"bytes"
"flag"
"fmt"
"log"
"runtime"
"time"
prt "github.com/alexbrainman/printer"
"github.com/jung-kurt/gofpdf"
)
func main() {
log.SetFlags(log.LstdFlags | log.Llongfile)
printFormat := flag.String("format", "pdf", "print the pdf content")
savePDF := flag.Bool("s", false, "save pdf only")
flag.Parse()
// pdf output
if *savePDF {
// create pdf
pdf := newReport()
// Save pdf to disc
pdf.OutputFileAndClose("report.pdf")
} else {
var buf []byte
var datatype string
// switch between formats
switch *printFormat {
case "text":
buf = []byte("Text report")
datatype = "text"
case "pdf":
// create pdf
pdf := newReport()
var b bytes.Buffer
err := pdf.Output(&b)
if err != nil {
fmt.Println(err)
}
buf = b.Bytes()
datatype = "raw"
}
// send content to printer
fmt.Println(string(buf))
printContent(datatype, buf)
}
}
func newReport() *gofpdf.Fpdf {
pdf := gofpdf.NewCustom(&gofpdf.InitType{
UnitStr: "mm",
Size: gofpdf.SizeType{Wd: 62, Ht: 90},
OrientationStr: "P",
})
// Pagebreak
pdf.SetAutoPageBreak(false, 0)
// Fileinformations
pdf.SetTitle("Test PDF", true)
// Page Margin
pdf.SetMargins(1, 1, 1)
// We start by adding a new page to the document.
pdf.AddPage()
// UTF8 from File
trans := pdf.UnicodeTranslatorFromDescriptor("")
// Title
pdf.SetFont("Arial", "B", 6)
pdf.CellFormat(0, 2, trans("Test PDF"), "", 0, "C", false, 0, "")
// The `Ln()` function moves the current position to a new line, with
// an optional line height parameter.
pdf.Ln(-1)
pdf.SetFont("Arial", "", 5)
pdf.CellFormat(0, 2, time.Now().Format("02.01.2006 15:04"), "", 0, "C", false, 0, "")
pdf.Ln(-1)
pdf.SetFont("Arial", "B", 5)
pdf.CellFormat(0, 2, trans("new text in pdf"), "", 0, "C", false, 0, "")
pdf.Ln(3)
return pdf
}
func printContent(datatype string, content []byte) {
if runtime.GOOS == "windows" {
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", datatype)
if err != nil {
fmt.Println(err)
}
err = p.StartPage() // begin a new page
if err != nil {
fmt.Println(err)
}
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)
}
}
}
Usage:
- Save pdf only
go run main.go -s
- Send text to printer
go run main.go -format=text
- Send pdf content to printer
go run main.go -format=pdf