dtrhd2850 2019-06-04 21:44 采纳率: 0%
浏览 1232

如何使用Golang在网络打印机上打印文件或格式文本

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:

  • PDF - github.com/jung-kurt/gofpdf
  • Printer - github.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
  • 写回答

1条回答 默认 最新

  • ok201211 2022-06-28 21:09
    关注

    测试下来打印机没有执行打印嘛

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名