You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
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
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.
The text was updated successfully, but these errors were encountered:
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.
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
and code below
```
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.
The text was updated successfully, but these errors were encountered: