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

Print a file if it has less than 8 pages in Windows #328

@MarErm27

Description

@MarErm27

Hello everyone! This is my first question at Github!
My task is to check how many pages contain a pdf document and if it has less than 8 pages print it silently. I'm using this imports

	"github.com/jung-kurt/gofpdf"
	"github.com/jung-kurt/gofpdf/contrib/gofpdi"

and code below
```

                       var err error

			pdf := gofpdf.New("P", "mm", "A4", "")

			pdfBytes, err := ioutil.ReadFile(DocName)
			if err != nil {
				panic(err)
			}
			rs := io.ReadSeeker(bytes.NewReader(pdfBytes))
			// Import in-memory PDF stream with gofpdi free pdf document importer
			imp := gofpdi.NewImporter()

			// import first page and determine page sizes
			tpl := imp.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
			pageSizes := imp.GetPageSizes()
			nrPages := len(imp.GetPageSizes())

			// add all pages from template pdf
			for i := 1; i <= nrPages; i++ {
				pdf.AddPage()
				if i > 1 {
					tpl = imp.ImportPageFromStream(pdf, &rs, i, "/MediaBox")
				}
				imp.UseImportedTemplate(pdf, tpl, 0, 0, pageSizes[i]["/MediaBox"]["w"], pageSizes[i]["/MediaBox"]["h"])
			}
			pdf.OutputFileAndClose(DocName + "test")

			pages := pdf.PageCount()
			fmt.Println(pages)

nrPages and pages is a places where I'm trying to get number of pages. And it's not always gives me proper number, but I really need it, sometimes I'm receiving files that contain up to 800 pages and I don't want to print them. I'm new at go, using it only for a week and maybe I just did something wrong. Here is a two examples of files, one returns proper number of pages the other one is giving a wrong number. The result files is those that I'm saving by trhis:
pdf.OutputFileAndClose(DocName + "test")
Incorrect number of pages.pdf
Incorrect result.pdf
Proper number of pages.pdf
Proper result (2).pdf

Next step is to print it. So I've found solution with cmd and Acrobat Reader DC. It's here "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /t D:\reports\print.pdf" "HP LaserJet 1018" "oem34.inf" "USB001".

Or maybe I will use a browser in kiosk-printing regime and browser extension that will do window.print().

So far I haven't found any better solution, but I'm going to dig it somehow. Thank you for your attencion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions