boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
734 stars 156 forks source link

Font color becoming white #145

Closed SubhaPrince closed 6 years ago

SubhaPrince commented 6 years ago

I've generate a pdf template using mac page/google doc

screen shot 2018-09-10 at 13 21 17

I've another pdf for content screen shot 2018-09-10 at 13 23 23

But when i am merging them that time text font color of a content file is getting white

The combined pdf screen shot 2018-09-10 at 13 23 48

Here is my sample code

dc = DocumentTemplate.find(5)
file1 = Tempfile.new(['file1', '.pdf'])
File.open(file1.path.to_s, 'wb') do |f|
  f.write(Base64.decode64(dc.body))
end

file2 = Tempfile.new(['file2', '.pdf'])
File.open(file2.path.to_s, 'wb') do |f|
  html_content = dc.description
  pd = WickedPdf.new.pdf_from_string(html_content,
                                     :margin => { :top => params[:top_margin] || 40, :bottom => 
                                                          params[:bottom_margin] || 10  },
                                     :layout => 'samdemo2.html',
                                     :zoom => 2.5,
                                     :print_media_type  => true,
                                     :page_size => 'A4',
                                     formats: :html, encoding: 'utf8'
                                     )
  f.write(pd)
end
company_logo = CombinePDF.load(file1.path).pages[0]
pdf = CombinePDF.load file2.path
pdf.pages.each {|page| page << company_logo} 
pdf.save "final.pdf"

Thanks

boazsegev commented 6 years ago

Hi @SubhaPrince ,

Thank you for opening this issue. I hope we can find a fast solution.

Could you send me the example PDFs so I can re-create the issue on my system?

Also, two question:

  1. It's possible that the white background is actually an object (opaque rather than transparent).

    Could test for this possibility?

    Perhaps you could test if the order of the PDF attachment effects the result (the PDF "on top" hiding the one below it)...?

  2. Which version of CombinePDF are you running? Can you make sure the issue occurs on the latest version (1.0.15)?

Kindly, Bo.

SubhaPrince commented 6 years ago

Hi, @boazsegev Thanks for your quick respond, i've attach the simple PDF file created by MAC pages.

PDF_created_by_MAC_pages.pdf

boazsegev commented 6 years ago

@SubhaPrince , thank you for the example PDF.

I can recreate the issue and testing it. I'll keep you updated once I know more.

Bo.

boazsegev commented 6 years ago

I can confirm that the issue is with the example PDF.

For some reason, macOS prints a white background object (box), preventing the background from being transparent.

Here is a way to see for yourself:

# make a new PDF, fill it with a blue box.
pdf = CombinePDF.new
pdf.new_page
pdf.pages[0].textbox("Blue Box", box_color: [0,0,0.7], font_color: [1,1,1])
# Save a reference / control PDF as a control group for the experiment
pdf.save "1-control.pdf"
# load the "white font"  PDF_created_by_MAC_pages.pdf
 pdf2 = CombinePDF.load("~/Desktop/white_font.pdf")
# imprint the white page on top of the blue page
pdf.pages[0] << pdf2.pages[0]
# The saved PDF shows a blue stripe where the white background (margins) don't cover the blue.
pdf.save '1.pdf'

As you will see, due to differences in page sizes, the resulting PDF will have a thing blue stripe on the right margin where the white background isn't overlapping the blue box.

Kindly, Bo

SubhaPrince commented 6 years ago

@boazsegev exactly that's the issue. Can we set the example pdf(created by mac) to the background and write the content on temple ?

boazsegev commented 6 years ago

@SubhaPrince ,

It's quite easy to reverse the position of the "imprinting" - simply replace the << operator with >>, or use the inject_page method.

Here's another example:

pdf = CombinePDF.new
pdf.new_page
pdf.pages[0].textbox("Transparent Example")
# Save a reference / control PDF as a control group for the experiment
pdf.save "1-control.pdf"
# load the "white font"  PDF_created_by_MAC_pages.pdf
pdf2 = CombinePDF.load("Desktop/white_font.pdf")
# imprint the white page beneath of the example PDF
pdf.pages[0] >> pdf2.pages[0]
# The saved PDF shows the example text above the white template.
pdf.save '1.pdf'

This example successfully imprints text on your example PDF.

Kindly, Bo

SubhaPrince commented 6 years ago

@boazsegev thanks for helping me.. Here is my solution

final_pdf = CombinePDF.new
company_template = CombinePDF.load(template_file.pdf).pages[0]
pdf = CombinePDF.load (content_file.pdf)
pdf.pages.each {|page| final_pdf << (company_template << page)} 
final_pdf.save "final_document.pdf"