boazsegev / combine_pdf

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

Russian language font #179

Closed 5anchezzz closed 4 years ago

5anchezzz commented 4 years ago

Hello! I have a problem with displaying a Russian language font. When I try to import a font from an existing PDF using the PDF#fonts method, i get empty array. fonts = CombinePDF.new("/Users/sanchez/decart/send-pdf/certificates-2/main-fix.pdf").fonts(true) => [] As far as I understand, the font in the pdf file is loaded and has type 0. image

image

Here is the file with the desired font main-fix.pdf

What am I doing wrong?

UPDATE: I was able to create a pdf file that gives Montserrat font on fonts = CombinePDF.new("montserrat_font.pdf").fonts(true) montserrat_font.pdf

But that also didn't solve the problem. After importing the font with CombinePDF.register_font_from_pdf_object :montserrat, fonts[0] it appears in the list of available: 2.6.5 :049 > CombinePDF::Fonts.fonts_list() => [:"Times-Roman", :"Times-Bold", :"Times-Italic", :"Times-BoldItalic", :Helvetica, :"Helvetica-Bold", :"Helvetica-BoldOblique", :"Helvetica-Oblique", :Courier, :"Courier-Bold", :"Courier-Oblique", :"Courier-BoldOblique", :Symbol, :ZapfDingbats, :montserrat ]

but when creating a textbox and explicitly setting the font, nothing appears in the pdf file.

It is very upsetting when you think that the problem is almost solved, but in fact nothing has changed)

boazsegev commented 4 years ago

Привет @5anchezzz ,

I noticed, when I run CombinePDF::Fonts.get_font(:montserrat).cmap, that I get:

CombinePDF::Fonts.get_font(:montserrat).cmap
# => {" "=>"20", "П"=>"21", "р"=>"22", "и"=>"23", "в"=>"24", "е"=>"25", "т"=>"26"}

This shows that the font only contains a small subset of the Russian alphabet. You might want to create a PDF file that has all the russian letters (preferably in order)... "абсдеф...АБСДЕФ...".

Then you will be able to use all the letters that the font provides - assuming your input has the same encoring as the letters in the cmap.

Good Luck - Удачи! Bo.

5anchezzz commented 4 years ago

Hello, @boazsegev ! Thank you very much for your reply! I created a pdf-file containing all the letters of the alphabet font-montserrat.pdf

and checked it your way. But this font cannot be passed to parameters when creating a textbox. image

code example

  def combine_cert
    cert_folder_path = "/Users/sanchez/decart/send-pdf/combine/certs/#{email}"
    FileUtils.mkdir_p(cert_folder_path) unless File.exist?(cert_folder_path)

    fonts = CombinePDF.new("/Users/sanchez/decart/send-pdf/combine/template/font-montserrat.pdf").fonts(true)
    CombinePDF.register_font_from_pdf_object :montserrat, fonts[0]

    pdf = CombinePDF.load "/Users/sanchez/decart/send-pdf/combine/template/main.pdf"
    pdf.pages[0].textbox("РУССКИЙ", { font: :montserrat })
    pdf.save "/Users/sanchez/decart/send-pdf/combine/certs/#{email}/main.pdf"
  end
boazsegev commented 4 years ago

There was an issue where the font doesn't publish the glyph's dimension properly.

I patched CombinePDF with a workaround the issue.

Please update to version 1.0.19.

I'm attaching the result for my attempt.

Good luck!

boazsegev commented 4 years ago

@5anchezzz - could you please let me know if 1.0.19 solved your problem? Can I close this issue?

5anchezzz commented 4 years ago

Awesome! Everything worked out! @boazsegev , thank you very much for your help! Great gem ... and great music just as well!!!

boazsegev commented 4 years ago

Thanks 🎉