printer icon indicating copy to clipboard operation
printer copied to clipboard

printer.go:43:27: undefined: syscall.Filetime

Open joeblew99 opened this issue 5 years ago • 1 comments

I am on darwin BTW. Might be because of this ?

clean first:

	go clean --modcache

example:

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)
        }
    }
}

joeblew99 avatar Oct 14 '19 11:10 joeblew99

I am on darwin BTW. Might be because of this ?

I am sure, that is the reason. This package is only designed to work on Windows.

If I try to build this on my Linux computer, I get the same error as you are.

$ go build -v -o /dev/null github.com/alexbrainman/printer
github.com/alexbrainman/printer
# github.com/alexbrainman/printer
src/github.com/alexbrainman/printer/printer.go:43:27: undefined: syscall.Filetime
src/github.com/alexbrainman/printer/printer.go:55:27: undefined: syscall.Filetime
src/github.com/alexbrainman/printer/printer.go:72:15: undefined: syscall.Systemtime
src/github.com/alexbrainman/printer/printer.go:155:4: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:38:21: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:50:35: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:62:24: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:74:22: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:86:21: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:98:25: undefined: syscall.Handle
src/github.com/alexbrainman/printer/zapi.go:98:25: too many errors
$ GOOS=windows go build -v -o /dev/null github.com/alexbrainman/printer
github.com/alexbrainman/printer
$

I don't plan extending this package to work on any non-Windows OS.

Alex

alexbrainman avatar Oct 16 '19 07:10 alexbrainman