Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Rendering issue where image content is blacked out #212

Closed
gunnsth opened this issue Dec 14, 2019 · 10 comments
Closed

[BUG] Rendering issue where image content is blacked out #212

gunnsth opened this issue Dec 14, 2019 · 10 comments
Assignees
Labels
bug Something isn't working render

Comments

@gunnsth
Copy link
Contributor

gunnsth commented Dec 14, 2019

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 gunnsth added the render label Dec 14, 2019
@gunnsth gunnsth added the bug Something isn't working label Jun 2, 2020
@gunnsth
Copy link
Contributor Author

gunnsth commented Jan 11, 2021

This is still a problem in v3.17.0.

@sarah-schmidt
Copy link

sarah-schmidt commented Jan 27, 2021

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
Copy link

sarah-schmidt commented Jan 28, 2021

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.

@gunnsth
Copy link
Contributor Author

gunnsth commented Jan 31, 2021

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

@sarah-schmidt
Copy link

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.

@sampila
Copy link
Collaborator

sampila commented Dec 19, 2023

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

@dillonkh
Copy link

dillonkh commented Jan 3, 2024

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...)

@sampila
Copy link
Collaborator

sampila commented Jan 4, 2024

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?

@dillonkh
Copy link

dillonkh commented Jan 5, 2024

@sampila that did the trick! 🙌

@sampila
Copy link
Collaborator

sampila commented Mar 7, 2024

Hi @dillonkh,

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

Best regards

@sampila sampila closed this as completed Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working render
Projects
None yet
Development

No branches or pull requests

5 participants