unipdf icon indicating copy to clipboard operation
unipdf copied to clipboard

[BUG] Rendering issue where image content is blacked out

Open gunnsth opened this issue 5 years ago • 9 comments

Description

When rendering the attached PDF, the raster image is blacked out, as can be seen below.

Expected Behavior

Expected rendering: image

Actual Behavior

I used the PDF rendering example: https://github.com/unidoc/unipdf-examples/blob/render/render/pdf_image_render.go to render the file 004_text_display.pdf and got: 004_text_display_1

Attachments

004_text_display.pdf

gunnsth avatar Dec 14 '19 18:12 gunnsth

This is still a problem in v3.17.0.

gunnsth avatar Jan 11 '21 17:01 gunnsth

This might be the same or similar issue that I am experiencing. When I run the example.pdf file through the pdf_image_render.go example, it creates a png image with a square-ish shaped blob at the top left corner.

example.pdf example_1

I ran the OP's pdf through as well and got something different than the OP stated, but still not expected. Attached for convenience.

004_text_display_1

sarah-schmidt avatar Jan 27 '21 22:01 sarah-schmidt

Update: pdf was rendering in my code base just fine after upgrading go to version 1.14 and upgrading unidoc/unipdf to v3.18.0. I did not try it in the unipdf-example.

sarah-schmidt avatar Jan 28 '21 00:01 sarah-schmidt

The originally posted file still having a problem in v3.18.0.

gunnsth avatar Jan 31 '21 21:01 gunnsth

Second update: pdf is rendering, but formatting is off and words are different sizes or cut off. PDF file and PNG image rendered through unipdf-examples pdf_image_render attached for reference single.pdf single_1

I ran the OP's PDF through as well and got the same result as OP.

I am using Go version 1.14 and unipdf v3.19.0.

sarah-schmidt avatar Feb 17 '21 20:02 sarah-schmidt

Hi @sarah-schmidt,

We released new UniPDF version v3.53.0 https://github.com/unidoc/unipdf/releases/tag/v3.53.0, could you try to update the UniPDF version and see if this issue is resolved?

Best regards, Alip

sampila avatar Dec 19 '23 08:12 sampila

Hi @sampila
I have been running into the same problem with v3.53.0. I stepped back versions from v3.53.0 to try and see if it was a new bug and turns out v3.51.0 does not have this issue, but v3.52.0 and v3.53.0 do.
The catch is v3.51.0 does not seem to support decoding and encoding with JPX 😞

p, err := pdf.NewPdfReader(r)
if err != nil {
	return nil, err
}

numPages, err := p.GetNumPages()
if err != nil {
	return nil, err
}

c := creator.New()

for i := 1; i <= numPages; i++ {
	// get page from pdf
	page, err := p.GetPage(i)
	if err != nil {
		return nil, err
	}
        
	s, err := c.NewImageFromGoImage(img) // img is a goimage (png with transparency)
	if err != nil {
		return nil, err
	}

	// add page to new pdf
	c.AddPage(page)

	err = c.Draw(s)
	if err != nil {
		return nil, err
	}
}

// create output buffer
b := new(bytes.Buffer)

// write pdf to output buffer
c.Write(b)

return b, nil

v3-51-0.pdf v3-53-0.pdf original-img (NOTE the web viewer may make the pdf look even worse...)

dillonkh avatar Jan 03 '24 18:01 dillonkh

Hi @dillonkh

From version v3.52.0, we changes the default encoder to DCTEncoder, to solve this, you can set the encoder into flate encoder, the code will be like this

p, err := pdf.NewPdfReader(r)
if err != nil {
	return nil, err
}

numPages, err := p.GetNumPages()
if err != nil {
	return nil, err
}

c := creator.New()

for i := 1; i <= numPages; i++ {
	// get page from pdf
	page, err := p.GetPage(i)
	if err != nil {
		return nil, err
	}
        
	s, err := c.NewImageFromGoImage(img) // img is a goimage (png with transparency)
	if err != nil {
		return nil, err
	}
        
        // Set go image encoder to Flate encoder.
        s.SetEncoder(core.NewFlateEncoder())

	// add page to new pdf
	c.AddPage(page)

	err = c.Draw(s)
	if err != nil {
		return nil, err
	}
}

// create output buffer
b := new(bytes.Buffer)

// write pdf to output buffer
c.Write(b)

return b, nil

Could you try to run the code and see the results?

sampila avatar Jan 04 '24 11:01 sampila

@sampila that did the trick! 🙌

dillonkh avatar Jan 05 '24 15:01 dillonkh

Hi @dillonkh,

As this is resolved, we closing this issue for now.

Best regards

sampila avatar Mar 07 '24 10:03 sampila