You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
}
}
}
The text was updated successfully, but these errors were encountered:
I am on darwin BTW. Might be because of this ?
clean first:
example:
The text was updated successfully, but these errors were encountered: