phpdave11/gofpdi

panic: Failed to get page rotation

Closed this issue · 1 comments

This is a good question. I wrote a small program to test whether this can be done but I ran into the following problem with gofpdi:

panic: Failed to get page rotation: Failed to get page rotation for parent: No parent for page rotation

@phpdave11, do you know why this happens? Here is the PDF I read as a template:

func samplePDF() (fileStr string, err error) {
  pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "")
  pdf.AddPage()
  pdf.SetFont("Arial", "B", 16)
  pdf.Cell(40, 10, "Hello World!")
  fileStr = "hello.pdf"
  err = pdf.OutputFileAndClose(fileStr)
  return
}

The generated PDF is smaller than the size of the buffer (1500) that is read, so the following change was made to gofpdi:

diff --git a/reader.go b/reader.go
index 3376b45..afdaff9 100644
--- a/reader.go
+++ b/reader.go
@@ -536,11 +536,20 @@ func (this *PdfReader) resolveObject(objSpec *PdfValue) (*PdfValue, error) {
 
 // Find the xref offset (should be at the end of the PDF)
 func (this *PdfReader) findXref() error {
+       const bufSize = 1500
        var result int
        var err error
        var toRead int64
 
        toRead = 1500
+       info, err := this.f.Stat()
+       if err != nil {
+               return errors.Wrap(err, "Failed to obtain file information")
+       }
+       toRead = info.Size()
+       if toRead > bufSize {
+               toRead = bufSize
+       }
 
        // 0 means relative to the origin of the file,
        // 1 means relative to the current offset,

Originally posted by @jung-kurt in jung-kurt/gofpdf#261 (comment)

Fixed by #4