shairontoledo / rghost

RGhost is a document creation and conversion API. It uses the Ghostscript framework for the format conversion, utilizes EPS templates and is optimized to work with larger documents. Support(PDF,PS,GIF,TIF,PNG,JPG,etc)
http://rghost.rubyforge.org
MIT License
187 stars 46 forks source link

External fonts seem unrecognized while converting to image #40

Closed josal closed 11 years ago

josal commented 11 years ago

Hi,

We're using rghost to convert a multipage PDF to JPGs this way:

RGhost::Convert.new(tmpfile).to :jpg, multipage: true, resolution: resolution

The problem is that sometimes the source PDF tmpfile doesn't have the fonts embedded. I this case, we have two options: first one, expect the fonts are installed in the system where we're making the conversions, or second one, more reliable, previously install the not-embedded font as an external font for rghost, following this article: https://github.com/shairontoledo/rghost/wiki/Using-external-fonts-with-Fontmap

We've followed these steps but the final JPGs never use the correct font at all. So, what's the proper way to install external fonts in order to use them in the RGhost::Convert(file).to method? Maybe does it have to do with the names in the Fontmap file (incorrect)? How can we know if the listed font is available from rghost? And finally, maybe do we have to do anything else for the "to" method to recognize the fonts?

Thanks.

shairontoledo commented 11 years ago

Hi José,

There's a method to list all GS loaded fonts

  RGhost::Config.enviroment_fonts.render :pdf, :filename => "/tmp/loaded_fonts.pdf"

Ref http://rghost.rubyforge.org/rdoc/classes/RGhost/Config.html

Are your fonts listed on it?

Best,

josal commented 11 years ago

Hi Shairon,

thanks for your reply, it encourages me a lot to keep on finding a solution! :-)

In my machine, this method generates a PDF with only 4 fonts, while the font book (it's a mac) shows many more. Moreover, it throws an error in the ruby console: GPL Ghostscript 9.07: Unrecoverable error, exit code 1 - how could I see more details about it?

In our production environment is not as easy to get dinamically-generated file contents, that's way I ask to you for a method that shows the GS loaded fonts list via ruby console, or even calling directly to GS in a shell. Viewing the loaded_fonts.pdf in production is difficult for us, althought the process is not throwing any errors like in my machine. Here the GS version is 8.71.

Waiting for your wise answers,

Jose.

shairontoledo commented 11 years ago

You can get the processing details through logfile and debug args. Like the following:

RGhost::Config.enviroment_fonts.render :pdf, :filename => "mycatalog.pdf", :debug => true, :logfile => "/tmp/logfile.txt" 

Fire the following command in your shell, I'll print loaded fonts

 echo "Fontmap { pop == } forall" | gs | grep '^/' | sort

You could also dig deeper your way looking for -sFONTPATH argument http://pages.cs.wisc.edu/~ghost/doc/gnu/5.50/Use.htm , it'll easier to debug it

 echo "Fontmap { pop == } forall" | gs -sFONTPAHT="$GS_FONTPATH:/path/to/another/place" | grep '^/' | sort

Best,

josal commented 11 years ago

Ok,

once I activate the debug mode in environment_fonts, the logfile is empty, and this in the info returned:

GPL Ghostscript 9.07: Unrecoverable error, exit code 1
/usr/local/bin/gs  -dNOPAUSE -dBATCH -dQUIET -dNOPAGEPROMPT  -I/Users/jose/.rvm/gems/ruby-2.0.0-p195/gems/rghost-0.9/lib/rghost/ps -I/Users/jose/Sites/myapp/app/fonts -sDEVICE=pdfwrite -sstdout=mycatalog.pdf.rgerr -sOutputFile=mycatalog.pdf -dDEVICEWIDTHPOINTS=595 -dDEVICEHEIGHTPOINTS=842 mycatalog.pdf.rgin

This is the content of mycatalog.pdf.rgin:

  (type.ps) runlibfile
  (unit.ps) runlibfile
  (IsoLatin.enc) runlibfile
  /default_encoding IsoLatin def  
<< /Duplex false /Tumble false >> setpagedevice
/pagesize [595 842]  def
/margin 3 dict def margin begin /top 1 cm  def /right 1 cm  def /bottom 2.3 cm  def /left   2.3 cm  def end
(paper.ps) runlibfile
   [ /Producer (Ruby Ghostscript - RGhost v0.8.7)  /DOCINFO pdfmark   /rows_per_page 80 def  /count_pages 10 def  /row_height 0.4 cm  def  /row_padding 0.1 cm  def    (basic.ps) runlibfile
  (cursor.ps) runlibfile
  (rectangle.ps) runlibfile
  (font.ps) runlibfile
  (textarea.ps) runlibfile
  (horizontal_line.ps) runlibfile
  (vertical_line.ps) runlibfile
  (callbacks.ps) runlibfile
  (show.ps) runlibfile
  (eps.ps) runlibfile
  (jpeg.ps) runlibfile
  (gif.ps) runlibfile
  (begin_document.ps) runlibfile
  (datagrid.ps) runlibfile
  (text.ps) runlibfile
  (frame.ps) runlibfile
  (link.ps) runlibfile
  (rect_link.ps) runlibfile
    limit_left  current_row  moveto  
/_span{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_b{
 /Helvetica-Bold encoding_font
/Helvetica-Bold-encoding exch definefont pop
/Helvetica-Bold-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_bold{
 /Helvetica-Bold encoding_font
/Helvetica-Bold-encoding exch definefont pop
/Helvetica-Bold-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_normal{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_i{
 /Helvetica-Oblique encoding_font
/Helvetica-Oblique-encoding exch definefont pop
/Helvetica-Oblique-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_bi{
 /Helvetica-BoldOblique encoding_font
/Helvetica-BoldOblique-encoding exch definefont pop
/Helvetica-BoldOblique-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/_big{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 10 scalefont setfont  0 setgray 
} bind def 

/_small{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 7 scalefont setfont  0 setgray 
} bind def 

/_h1{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 14 scalefont setfont  0 setgray 
} bind def 

/_h2{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 13 scalefont setfont  0 setgray 
} bind def 

/_h3{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 12 scalefont setfont  0 setgray 
} bind def 

/_h4{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 11 scalefont setfont  0 setgray 
} bind def 

/_h5{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 10 scalefont setfont  0 setgray 
} bind def 

/_title{
 /Helvetica encoding_font
/Helvetica-encoding exch definefont pop
/Helvetica-encoding findfont 20 scalefont setfont  0 setgray 
} bind def 

/_pre{
 /Courier encoding_font
/Courier-encoding exch definefont pop
/Courier-encoding findfont 8 scalefont setfont  0 setgray 
} bind def 

/before_page_create 3 dict def before_page_create begin 
/proc {   BeginEPSF 
 0 cm  0 cm  translate  
    (/Users/jose/.rvm/gems/ruby-2.0.0-p195/gems/rghost-0.9/lib/rghost/ps/rghost_default_template.eps) run 
EndEPSF
    } bind def 
/except []  def 
/only []  def 
end   (begin_document.ps) runlibfile
  limit_left  current_row  moveto   beging  _default_font (Search Path)  show_left  endg  gsave  0.5 setlinewidth  0 setlinejoin 0 setlinecap 0.28627450980392155 0.6666666666666666 0.9803921568627451 setrgbcolor   limit_left  area_x  horizontal_line_bottom grestore  nrdp  default_font 
    LIBPATH{
      limit_left  current_row  moveto   show
      nrdp
    } forall

     nrdp  beging  _default_font (Example)  show_left  endg  13 cm  current_row  moveto  beging  _default_font (Font Name)  show_left  endg  gsave  0.5 setlinewidth  0 setlinejoin 0 setlinecap 0.28627450980392155 0.6666666666666666 0.9803921568627451 setrgbcolor   limit_left  area_x  horizontal_line_bottom grestore  nrdp  
      Fontmap{
        50 string cvs pop dup findfont 10 scalefont setfont
        limit_left  current_row  moveto  (The quick brown fox jumps over the lazy dog)  show
    13 cm  current_row moveto default_font  0 setgray
    50 string cvs show
    nrdp
      } forall

 after_page_create  callback  after_document_create  showpage  
%%EOF  

Regarding the other command, I've executed:

echo "Fontmap { pop == } forall" | gs -sFONTPAHT="$GS_FONTPATH:app/fonts" | grep '^/' | sort

where app/fonts folder contains a working custom font, but it is not shown in the list, even when I know from Font Book (mac) that it's installed correctly.

What else can I try? Thanks a lot indeed!!

josal commented 11 years ago

Finally I've solved the problem. It's important to specify the correct font name in the Fontmap file. Now it's working:

http://stackoverflow.com/a/16593482/717506

josal commented 11 years ago

Shairon, now that I can detect and use any not-embedded font in any PDF, would you know how to embed this font in the original PDF file? Is there a method for this?

I could use the gs command line tool, like here: http://stackoverflow.com/a/4234150/717506, but is there maybe an interface from the rghost gem instead of calling to system() ?

Thanks again.

josal commented 11 years ago

A simple call to convert takes the new included fonts in Fontmap and embedded them succesfully:

RGhost::Convert.new(tmpfile).to :pdf, :filename => tmpfile_embedded