alexbrainman / printer Goto Github PK
View Code? Open in Web Editor NEWWindows printing
License: BSD 3-Clause "New" or "Revised" License
Windows printing
License: BSD 3-Clause "New" or "Revised" License
如何打印pdf文档
Building cmd/print with the -gcflags=all=-d=checkptr
option causes a panic when running print.exe -l
print.exe -l
fatal error: checkptr: unsafe pointer conversion
goroutine 1 [running]:
runtime.throw(0x50d846, 0x23)
c:/go/src/runtime/panic.go:1116 +0x79 fp=0xc000107d20 sp=0xc000107cf0 pc=0x4332f9
runtime.checkptrAlignment(0xc000108000, 0x4dc5a0, 0x1)
c:/go/src/runtime/checkptr.go:20 +0xd0 fp=0xc000107d50 sp=0xc000107d20 pc=0x406820
github.com/alexbrainman/printer.ReadNames(0xc000010109, 0xc000010120, 0xc00001c100, 0xc000107ee8, 0x45dd05)
C:/MyGo/src/github.com/alexbrainman/printer/printer.go:145 +0xda fp=0xc000107e48 sp=0xc000107d50 pc=0x4c83ba
main.listPrinters(0xc00004a060, 0xc0000044b0)
C:/MyGo/src/github.com/alexbrainman/printer/cmd/print/print.go:36 +0x3b fp=0xc000107f28 sp=0xc000107e48 pc=0x4ca07b
main.main()
C:/MyGo/src/github.com/alexbrainman/printer/cmd/print/print.go:145 +0x197 fp=0xc000107f88 sp=0xc000107f28 pc=0x4caf67
runtime.main()
c:/go/src/runtime/proc.go:203 +0x212 fp=0xc000107fe0 sp=0xc000107f88 pc=0x4359c2
runtime.goexit()
c:/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc000107fe8 sp=0xc000107fe0 pc=0x45dc41
I'm not 100% clear why though
我执行了打印,文件发送到了打印队列,打印机显示等待接收数据,但是没打印没有打印
Hello, i was build https://play.golang.org/p/tI-7R98hHa , and it was does't work. i can see task to printer, but printer not work. any one can help me?
my os is windows 10, go version go1.9.2 windows/amd64
my printer is network printer.
I'm facing a problem where I'm trying to send a job to print a RAW file to the printer. The job is actually created in the printer jobs list, but the status keeps "Printing" but don't actually start printing.
I thought it could be a misconfiguration on the printer itself, but when I try to print any other document using other program, it prints normally.
I don't know if anyone has experienced that and how have you overcome this, any tips would be appreciated.
You can check my code at Feggah/calculator/printer/printer.go
Hi Alex, when I use the print machine with windows directly, even if the document has only one line the print machine will print the line and then end the printing, then send the whole A4 paper out.
But when I use this lib, the print machine will print the one line but the A4 paper is still in the machine, so I can not get it out.
My question is that how to use this lib to make the print machine send out the A4 paper after my printing?
Any chance for some examples ? the only example, how to use your package can be found here: https://stackoverflow.com/questions/29491148/go-how-can-i-print-to-an-actual-printer/29493081 any chance to get more examples ? if it's possible to print image for example ?
I'm getting an "The data is invalid" error when calling StartRawDocument ... this was working some weeks ago, so I think it might be caused by some OS upgrade.
¿Any idea?
I'm under W11.
Alluded to here:
I'm not 100% sure about this, and it's more to pick @alexbrainman brain as I think you'll have better insight into this than me.
Functions such as https://docs.microsoft.com/en-us/windows/win32/printdocs/getprinter take a single chunk of memory to return an array of (for level 5, for example) PRINTER_INFO_5 structures. This are correctly represented as such:
type PRINTER_INFO_5 struct {
PrinterName *uint16
PortName *uint16
Attributes uint32
DeviceNotSelectedTimeout uint32
TransmissionRetryTimeout uint32
}
The way this works with the string pointers is that space is allocated for N PRINTER_INFO_5 structs + an additional amount of space at the bottom for the strings. The string pointers within the structs then point at the correct offset into that additional space.
This can be seen below (I added some debug):
buf extent C00007C000 -> C00007C558
p.PrinterName for printer 0 @ C00007C504
p.PrinterName for printer 1 @ C00007C4C8
p.PrinterName for printer 2 @ C00007C48C
p.PrinterName for printer 3 @ C00007C406
p.PrinterName for printer 4 @ C00007C3C0
p.PrinterName for printer 5 @ C00007C29A
p.PrinterName for printer 6 @ C00007C254
p.PrinterName for printer 7 @ C00007C20E
p.PrinterName for printer 8 @ C00007C1EE
p.PrinterName for printer 9 @ C00007C192
My concern is, (and I believe I'm correct here, but may not be so would love your input) the go runtime is free to move the address of the buf allocation? However the string pointers "within" the buf allocation would not change the address they are pointing at, so would no longer point to the correct place? As I understand it, that is why the cGo rules do not allow you to pass a pointer to a Go struct containing Go pointers into cGo? - this in effect is circumventing that with a byte buffer but since it's cast it's the same thing
If this is a problem how can it be addressed? I presume using an allocater like LocalAlloc would do the trick ensuring the memory is not in view of the Go runtime. Though I can't see anywhere in the standard library or x/sys/windows which exposes this. C.malloc a possibility, though that introduces an (undesirable?) CGo dependency.
Bypassing the printer driver and using the Windows Printer Spool API allows you to pass in a file path and print the file however it should be handled (covers PDF, DOCX, XLSX, txt files which would usually need different approaches when processed by the driver).
Particularly the AddJob function allows a job with a file path.
It feels like the last little part missing in this brilliant library!
Hello, is there an option to set Font Size? I couldn't find it. It would be great if there's an option to set Font Size. I want to use it as printer server that could be used to print to many printer. It's only need to set fontsize base on which printer is used.
Thank you for your great code.
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)
}
}
}
HI Alex,
my code as follows:
p, err := prt.Open(printName)
err = p.StartDocument("Printer", "RAW")
if err != nil {
log.Fatal(err)
}
err = p.StartPage() // begin a new page
if err != nil {
log.Fatal(err)
}
...
p.Write(decodeBytes) //decodeBytes is test data;
and the printer out nothing.
Any advice for me ?
Thank you very much.
In the CMD folder I can see it asks for a folder, and then gets the string lines of the file contents.
Does this support printing a pdf though ?
如何获取 PRINTER_INFO_2的信息,判断打印机卡纸状态。
Hi Alex,
I can not print Chinese character (got bad prints), but English is OK. What might be the problem?
This is my testing code:
package main
import (
"fmt"
"log"
"github.com/alexbrainman/printer"
)
func main() {
testWindowPrinter()
}
func testWindowPrinter() {
printLines("你好", "世界") // HERE, I got bad prints
printLines("hello", "world")
}
func printLines(lines ...string) error {
var defaultPrinter, err = printer.Default()
if err != nil {
log.Println(1, err.Error())
return err
}
p, err := printer.Open(defaultPrinter)
if err != nil {
log.Println(2, err.Error())
return err
}
defer p.Close()
err = p.StartRawDocument("test")
if err != nil {
log.Println(3, err.Error())
return err
}
defer p.EndDocument()
err = p.StartPage()
if err != nil {
log.Println(4, err.Error())
return err
}
for i := range lines {
fmt.Fprintf(p, "%s\r\n", lines[i])
}
err = p.EndPage()
if err != nil {
log.Println(5, err.Error())
return err
}
return nil
}
After installing the latest version f40f26f, darwin build fails for other packages. and the error message is "undefined: unix.SYS_IOCTL"
Reverting or removing this package from go.mod resolves the issue. This issue only occurs for darwin and not linux
I am trying to test this package using CutePDF virtual printer it always prints an empty pdf and when I try XPS printer it does not open the save dialog just icon in the tray tells printing completed but nothing happens
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.