-
Notifications
You must be signed in to change notification settings - Fork 39
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
How can I set a global default font for all PDFs generated with prawn-rails? #54
Comments
It would appear that prawn itself does not have this option. Prawn seems to explicitly default to "Helvetica" (defaults to it by name not font ordering)
So you would probably need to apply a monkey patch of some sort currently |
It kind of seems like prawn-rails is in a good position to apply a default font since we are the ones who have implemented I could see adding a configuration option for this. The only concern I see with changing |
@denmarkmeralpis I've created a PR for the configuration changes. Can you please try it out and see if it solves this issue for you. (see the instructions that were added in the README within the PR) Once I receive confirmation from you I will merge the PR and release a new version of the gem. |
It didn't work. It raises an exception |
Can you please post the stacktrace? |
Looks to me like thats an issue with your applications code. I dont think the PR or the gem has anything to do with that error. |
I don't believe the issue is with my code, as I followed the configuration exactly as outlined in the # config/initializers/prawn_rails.rb
PrawnRails.config do |config|
config.page_layout = :portrait
config.page_size = [8.5.in, 11.in]
config.skip_page_creation = true
config.additional_fonts = {
"some-custom-font" => {
normal: Rails.root.join('app/assets/fonts/print/some-custom-font.ttf'),
italic: Rails.root.join('app/assets/fonts/print/some-custom-font.ttf'),
bold: Rails.root.join('app/assets/fonts/print/some-custom-font-bold.ttf'),
bold_italic: Rails.root.join('app/assets/fonts/print/some-custom-font-bold.ttf')
}
}
# config.default_font_name = "some-custom-font"
end
# print.pdf.prawn
prawn_document do |pdf|
pdf.font 'some-custom-font'
end Anyways, here's the stacktrace:
|
Heres the cause config.skip_page_creation = true That setting throws a wrench in this solution. Will need to think how to handle it. |
What benefit does |
Im inquiring about the reason why the first page must exist to set the font, prawnpdf/prawn#1369 |
I've now updated to PR to monkey_patch |
Setting |
Sure but then isnt the very first thing you do is call Not sure why you'd create a PDF with no content. But its a valid config option, so we must support it I suppose. |
Yeah, you're right! What my PDF does is loop through multiple printable data, and each of them should start a new page so I can print them in bulk. For now, I'll skip calling printables = ['text 0', 'text 1', 'text 2]
prawn_document do |pdf|
printables.each_with_index do |printable, i|
pdf.start_new_page unless i.zero?
pdf.text printable
...
end |
Anyway, for now, I’ve applied a monkey patch and specified the # config/initializers/prawn_rails.rb
require 'prawn-rails/document'
PrawnRails.config do |config|
config.page_layout = :portrait
config.page_size = [8.5.in, 11.in]
config.skip_page_creation = true
end
# monkey patching PrawnRails::Document to add custom fonts
PrawnRails::Document.class_eval do
alias_method :orig_init, :initialize
def initialize(*args)
orig_init(*args)
font_families.update(
'some-font' => {
normal: Rails.root.join('app/assets/fonts/print/some-font.ttf'),
italic: Rails.root.join('app/assets/fonts/print/some-font.ttf'),
bold: Rails.root.join('app/assets/fonts/print/some-font-bold.ttf'),
bold_italic: Rails.root.join('app/assets/fonts/print/some-font-bold.ttf')
}
)
end
end
# print.pdf.prawn
prawn_document do |pdf|
# ...
font 'some-font'
# ...
end |
v1.6.0 is now released which contains this new feature You can feel free to keep your original |
Currently, I am able to set the font inside the
prawn_document
block like this:However, I want to set this font globally for all PDFs without having to specify it inside each
prawn_document
block. Is there a way to configure this globally for my entire application, ideally in anconfig/initializers/prawn_rails.rb
?Thanks!
The text was updated successfully, but these errors were encountered: