unipdf
unipdf copied to clipboard
[BUG] Rendering issue where image content is blacked out
Description
When rendering the attached PDF, the raster image is blacked out, as can be seen below.
Expected Behavior
Expected rendering:

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:

Attachments
This is still a problem in v3.17.0.
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.
I ran the OP's pdf through as well and got something different than the OP stated, but still not expected. Attached for convenience.

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.
The originally posted file still having a problem in v3.18.0.
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

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.
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
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
(NOTE the web viewer may make the pdf look even worse...)
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 that did the trick! 🙌
Hi @dillonkh,
As this is resolved, we closing this issue for now.
Best regards
