jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.31k stars 777 forks source link

Background transparency #261

Closed hugoglt closed 5 years ago

hugoglt commented 5 years ago

Hello I read #130 and #141 issue but I think that they don't contain the answer I'm looking for.

If you look this both pdf file in pdf reader you'll see the background like white but is not the same, the first have white background and the second doens't have background.

transparency-background.pdf white-background.pdf

I have a existing pdf file with an image, text and white background, I want define the background like transparency or delete the background (to stamp it in a second time)

The problem is I didn't found method to do this, maybe the method must be created or it's impossible ?

I saw SetAplha in the documentation but is not apply on the background ONLY

I saw CellFormat with fill parameter too but if I apply a transparency background on the white background the result is a white background

jung-kurt commented 5 years ago

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,
phpdave11 commented 5 years ago

@jung-kurt there were some bugs in the code that parses the page box and the page rotation, which I just fixed. Thanks for catching that.

phpdave11 commented 5 years ago

@jung-kurt also - thanks for finding the issue when the imported PDF is less than 1500 bytes. That bug has been fixed as well.

phpdave11 commented 5 years ago

@hugoglt is your goal to import white-background.pdf into a new gofpdf document, and remove the white background so that the output PDF is transparent? If so, I don't think that is possible. Your white-background.pdf document contains a white image that covers the entire PDF:

595.2 0 0 841.92 0 0 cm /Im0 Do

Gofpdi can import existing PDFs but it does not modify the page content stream, which the white background is part of.

phpdave11 commented 5 years ago

gofpdi v1.0.2 now supports importing linearized PDFs. I tested it with white-background.pdf and it works fine now.

jung-kurt commented 5 years ago

Please reopen if this is still an issue