Skip to content
This repository has been 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

Open
MarErm27 opened this issue Oct 23, 2019 · 2 comments
Open

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

MarErm27 opened this issue Oct 23, 2019 · 2 comments

Comments

@MarErm27
Copy link

MarErm27 commented Oct 23, 2019

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.

@jung-kurt
Copy link
Owner

My guess is that nrPages := len(imp.GetPageSizes()) is for some reason not returning the number of pages. (Any comment, @phpdave11?) I think pdf.PageCount() will always return the number of times pdf.AddPage() is called, and in this case, it will be limited to the value of nrPages.

Small hint unrelated to the issue: os.Open() returns an *os.File which implements the io.ReadSeeker interface. You could use this in your call to imp.ImportPageFromStream() instead of reading all bytes into one large array and then streaming them out with a *bytes.Reader.

@MarErm27
Copy link
Author

MarErm27 commented Oct 24, 2019

I came to this code

func isValueInList(value string, list []string) int {
	for i, v := range list {
		if strings.Contains(v, value) {
			return i
		}
	}
	return -1
}
		                out, err := exec.Command("pdfinfo.exe", DocName).Output()
				if err != nil {
					log.Fatal(err)
				}
				fmt.Printf("The date is %s\n", out)

				str := string(out)
				s := strings.Split(str, "\n")
				x := "Pages"
				indexOfPages := isValueInList(x, s)
				fmt.Println(indexOfPages)
				if indexOfPages != -1 {
					pages := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s[indexOfPages], "Pages:", ""), " ", ""), "\r", "")
					i, err := strconv.Atoi(pages)
					if err != nil {
						fmt.Println(err)
					}
					fmt.Println(i)
					fmt.Println(i < 8)
				}

pdfinfo.exe is from this open source project http://www.xpdfreader.com/download.html
Thank you very much

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

2 participants