Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Print a PDF to a physical printer #320

Open
joeblew99 opened this issue Oct 5, 2019 · 10 comments
Open

Print a PDF to a physical printer #320

joeblew99 opened this issue Oct 5, 2019 · 10 comments

Comments

@joeblew99
Copy link

Wondering if there is any information about how to managing printing on desktops in golang.

This is the sequence I am trying to achieve

After the user has selected a pdf template and my code has merged data into the pdf I want to let the user print it.

Opens the print dialogue for the OS.
User selects printer, orientation, paper side.
The orientation and paper size is used to defender the pdf correctly.
Then user is show new pdf
Then user confirms and prints
Then system send to print spooler.

Am looking for tips about how to access these OS specific APIs.
It's not PDF related but I would think others may find this useful.
Hoping I can work this out and put into contribution repo

@joeblew99
Copy link
Author

joeblew99 commented Oct 5, 2019

I think maybe the ghostscript features are needed to rasterise the pdf for viewing.

But it seems it can also open the printer dialogue and send the pdf to the printer ?

@jung-kurt
Copy link
Owner

It's not PDF related but I would think others may find this useful.

You're right -- physical printing is not a part of PDF production. If you want a solution for the Windows platform, the github.com/alexbrainman/printer project may be what you need. On Linux and Mac, I would look into Samba.

@joeblew99
Copy link
Author

It's not PDF related but I would think others may find this useful.

You're right -- physical printing is not a part of PDF production. If you want a solution for the Windows platform, the github.com/alexbrainman/printer project may be what you need.

The Alexbrainman only prints text, not PDF. I looked at his lib and spoke to him about about it already. Seems like a dead end.

On Linux and Mac, I would look into Samba.

Cant use this becaue i cant assume they have samba. It imposses too much on the users to have to install Samba.


The onyl way to do it properly is to open the native Print Preview dialog.
When the user selects and print and then paper size and orientation, you have to then rerender the PDF to that and give the Print Preview Dialog the preview image of the PDF.
This would be the ideal but its quite integrated with the native song and dance.

Trying to work out how to tap into this native stuff on Windows and Mac and linux.

@jung-kurt
Copy link
Owner

The onyl way to do it properly is to open the native Print Preview dialog. When the user selects and print and then paper size and orientation, you have to then rerender the PDF to that and give the Print Preview Dialog the preview image of the PDF.

Good point. This seems to point to some cross-platform GUI framework that has access to the native print dialog and can execute the PDF generation application. This application, which imports gofpdf, would accept parameters that would drive the PDF process.

@joeblew99
Copy link
Author

yep thats basically it.
I have not had time to dive into this yet though :)

@jacobalberty
Copy link
Contributor

@joeblew99 one option (if you're either on linux/osx or dealing with networked printers) is an ipp client like https://github.com/phin1x/go-ipp you'd have to build the print dialog yourself though

@joeblew99
Copy link
Author

@jacobalberty thanks will try it now.

@jacobalberty
Copy link
Contributor

I got this working with go-ipp so figured I'd post enough of my code here to help anyone else .
Fairly easy to use just instantiate IPPDriver with the appropriate settings (user and password can be blank) and provide it to your Fpdf.Output() then call IPPDriver.Print() function with a name for your document. If you want anything more advanced you would need to modify the code to fill out the third parameter of PrintJob see https://godoc.org/github.com/phin1x/go-ipp#IPPClient.PrintJob for information on that third parameter.

type IPPDriver struct {
        bytes.Buffer
        printer  string
        server   string
        port     int
        user     string
        password string
}

func (d *IPPDriver) Print(name string) (int, error) {
        doc := ipp.Document{
                Document: d,
                Size:     d.Buffer.Len(),
                Name:     name,
                MimeType: "application/pdf",
        }
        ippc := ipp.NewIPPClient(d.server, d.port, d.user, d.password, true)
        return ippc.PrintJob(doc, d.printer, map[string]interface{}{})
}

@joeblew99
Copy link
Author

i looked here and only found HP printer supported: https://www.pwg.org/dynamo/eveprinters.php
SO i got turned off the idea of using it.
I guess this is wrong and lots of printers support IPP ?

@jacobalberty
Copy link
Contributor

@joeblew99 cups as of 2.3 is deprecating printer drivers to rely on IPPE the logic being most printers sold after 2010 support the IPPE standard. So virtually any network printer should support IPP, not all will support PDF input I think I have a few older printers at work that claim not to support PDF. I'm doing all my printing through cups right now though which can act as an IPP endpoint for any printer cups supports (even with 2.3 printer drivers still work they're just deprecated)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants