Git Product home page Git Product logo

princely's Introduction

Princely

Princely is a simple wrapper for the Prince XML PDF generation library. The plugin will also automatically register the PDF MimeType so that you can use pdf as a format in Rails controller respond_to blocks.

Example

class Provider::EstimatesController < Provider::BaseController
  # You can render PDF templates simply by
  # using the :pdf option on render templates.
  def show
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => 'file_name',
               :template => 'controller/action',
               :handlers => %w[erb],
               :formats => %w[pdf],
               :stylesheets => %w[application prince],
               :layout => 'pdf',
               :locals => { :foo => 'bar' },
               :disposition => 'inline', # PDF will be sent inline, means you can load it inside an iFrame or Embed
               :relative_paths => true # Modify asset paths to make them relative. See [the AssetSupport module](/lib/princely/asset_support.rb)
      end
    end
  end

  # Alternatively, you can use make_and_send_pdf to render out a PDF for the
  # action without a respond_to block.
  def pdf
    make_and_send_pdf "file_name"
  end
end

Render Defaults

The defaults for the render options are as follows:

layout:          false
template:        the template for the current controller/action
locals:          none
stylesheets:     none
disposition:     attachment (created PDF file will be sent as download)
relative_paths:  true
server_flag:     true
javascript_flag: false
timeout:         none

Contributors

Resources

  • Copyright (c) 2007-2013 Michael Bleigh and Intridea, Inc., released under the MIT license.

princely's People

Contributors

andrewvos avatar brian4d avatar bwlang avatar cpjolicoeur avatar drnic avatar edwinv avatar hardbap avatar igreg avatar ilake avatar jbarket avatar jmckible avatar jondruse avatar modsognir avatar parndt avatar prakashmurthy avatar rdunlop avatar repinel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

princely's Issues

invalid byte sequence in UTF-8

Hello,

I have trying create a project using ruby1.9.1 and princely plugin.

When I generate pdf file the follows erros appears.

I try fix this, but I faill. Anybody can help me?

PRINCE XML PDF COMMAND
/usr/local/bin/prince --input=html --server --log=/home/ricardo/teste/log/prince.log --silent - -o -

Sending data file_name.pdf
Completed in 594ms (View: 5, DB: 0) | 200 OK [http://192.168.100.193/posts/1.pdf]

Processing ApplicationController#show to pdf (for 192.168.100.170 at 2010-03-18 09:51:13) [GET]
Parameters: {"id"=>"1"}

ArgumentError (invalid byte sequence in UTF-8):
internal:prelude:8:in synchronize' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:inservice'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /usr/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread'

Windows XP support

mikedc55 (http://www.nullislove.com/2007/05/30/easy-pdfs-from-rails/) commented a fix for running Princely on Windows XP.

Was wondering if you could either figure out a way to get the done on plugin install, or at least include it in the README file.

He wrote:

I got it to work…finally.
I edited the prince.rb file as follows:

  • @exe_path = “C:\Program Files\Prince\Engine\bin\prince” .chomp
  • uncommented the logger method because it gave me errors. (had this same error on osx as well)
  • plus I added the following line in the pdf_from_string method;
    pdf.binmode

so now it’s:
pdf = IO.popen(path, “w+”)
pdf.binmode

…etc…

Rails integration eagerly loads ActionController::Base

Rails integration refers to ActionController::Base directly. This breaks lazy loading for all controllers and causes Rails 6 to want about eagerly loaded Constants, e.g.

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload ActionText::ContentHelper, for example,
the expected changes won't be reflected in that stale Module object.

These autoloaded constants have been unloaded.

Please, check the "Autoloading and Reloading Constants" guide for solutions.

This also causes occasional errors in development when constants should change but do not due to improper load order.

Rails 4.2.1 Images do not show in generated pdf

I have a bunch of images in app/assets/images/ when I render the html view the images are present as one would expect. When I render the pdf view via princely they are not present I just get something like this: Bd header70 b cc0e9cd02bd0b2ec1f31f22bbfe90be47fec42de002e9f5445603b82b492bdaa

In my controller I have this:

    render :pdf => 'ConsultantsQuote',
           :template => 'quotes/show_consultant',
           :handlers => %w[erb],
           :formats => %w[pdf],
           :stylesheets => %w[application pdf],
           :layout => 'pdf',
           :disposition => 'inline', 
           :relative_paths => true 

In my view:

<%= image_tag("bd_header70_b.png") %>

What am I doing wrong? Thanks Paul Thompson

upload image not wokring

Hi,

Image not showing in the pdf after upload image and generate pdf.

<%= image_tag(url_for_file_column(@user,"signature", "small")) %>

How to pix this issue???

Server exception: NoMethodError (undefined method `bytesize' for nil:NilClass)

Hello,
I am trying to use your library, however I have some problems.
Locally in my development environment, everything works perfectly,
but when I deploy it on the server I have the following below bug.
The ruby version and rails version are the same of both local and server machine.

Have you get into such thing ever?
Seem like a problem is with send_data function
but I can't figure it out

Thank you

Evgeny

Processing PrintController#print to pdf (for ______ at 2010-07-11 23:12:11) [GET]
Parameters: {"format"=>"pdf", "month"=>"7", "action"=>"print", "year"=>"2010", "controller"=>"print"}
Rendering print/print.pdf.erb

PRINCE XML PDF COMMAND
/usr/bin/prince --input=html --server --log=/home/vohtaski/public_html/trainandrace.com/releases/20100711230942/log/prince.log -s /home/vohtaski/public_html/trainandrace.com/releases/20100711230942/public/stylesheets/prince.css --silent - -o -

Sending data plans.pdf

NoMethodError (undefined method bytesize' for nil:NilClass): app/controllers/print_controller.rb:137:inprint'
app/controllers/print_controller.rb:128:in print' haml (2.2.20) lib/sass/plugin/rails.rb:20:inprocess'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/rack/request_handler.rb:92:in process_request' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_request_handler.rb:207:inmain_loop'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/railz/application_spawner.rb:400:in start_request_handler' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/railz/application_spawner.rb:351:inhandle_spawn_application'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/utils.rb:184:in safe_fork' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/railz/application_spawner.rb:349:inhandle_spawn_application'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:352:in __send__' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:352:inmain_loop'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:196:in start_synchronously' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:163:instart'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/railz/application_spawner.rb:209:in start' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/spawn_manager.rb:262:inspawn_rails_application'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server_collection.rb:126:in lookup_or_add' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/spawn_manager.rb:256:inspawn_rails_application'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server_collection.rb:80:in synchronize' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server_collection.rb:79:insynchronize'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/spawn_manager.rb:255:in spawn_rails_application' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/spawn_manager.rb:154:inspawn_application'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/spawn_manager.rb:287:in handle_spawn_application' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:352:insend'
/usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:352:in main_loop' /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/abstract_server.rb:196:instart_synchronously'

make_and_send_pdf should accept :disposition as an option

Sometimes you want to display the pdf inline so people don't have to constantly download files just to print them.

I patched the send_data call like this (Probably a more attractive way to do it)
:disposition => options.has_key?(:disposition)?options[:disposition]:"attatchment"

A new release?

Would it be possible to do a version bump? A lot has changed since 1.4.1.

The file doesn't start with '%PDF-'

Hello
I got a wamp on a windows 7, with rails 3, ruby 1.8.7

I installed the plugin in my application, and it just keeps telling me "Le fichier ne commence pas par '%PDF-'" in French, wich means the pdf file created doesnt start with '%PDF-'

Anyone has an idea?

I must say I have big trouble understanding what this plugin (or Gem???) needs... i found it needs jewler, but this Prince XML i installed is still needed? and if it is really $3800 for a server licence, i'll look for another way of doing it.

Hope you can help.

cheers, thanks in advance

get pdf as base64-encoded string?

I've been happily using prince/princely in a Rails 4 app to render PDFs which can be downloaded in the browser, using a format.pdf block in a controller—all as described in the README.

Now I'm looking to construct a PDF in the same way (using a Rails view template), but I want to invoke the pdf creation in a model, not a controller. Instead of emitting the file, I want to capture it as a string in my code, so I can then base64-encode it and pass it off to some unrelated code for further processing.

Can you provide any pointers about how I render the pdf to a string from a model—while still making use of my view? Thanks much!

Rails assets pipeline

Hello, working in rails 3.2 here what the best I found to make the app work with the assets pipeline, I am embedding the stylesheets content in the html instead of adding a link to it.

def prince_stylesheet_link_tag(*sources)
sources.each.collect do |source|
asset = Rails.application.assets.find_asset(source)
if asset.nil?
raise "could not find asset for #{source}.css"
else
content_tag :style, asset.body, { :type => 'text/css' }
end
end.join("\n").html_safe
end

This work well in development and production.

Haven't found a good way to make images work yet, any clue would be appreciated.

Pierre-Marc

HTML from Layout and template files is not passed into pdf file

In case you're encountering this same problem. I'm using Rails 3.2.8 and Ruby 1.9.2.

I don't know if I was doing something wrong, but it took me a while to realize that the HTML that was passed to princeXML through the princely plugin was not pulling anything from the layout or template options that were passed in using render :pdf in the controller. In other words, the only HTML that was passed to Prince was whatever was in my view, but nothing from the layout. I had the same problem using mark_and_send_pdf().

To fix I just put my template in my show.pdf.html file. Not an ideal solution, but it works.

Brad

Stylesheet paths

Hello,

I'm using Rails 5.1.4 and I can't get the stylesheet to work due to the path produced by Princely.

As the app is not in production yet, the stylesheet doesn't exist (in public) and even with rails assets:precompile the path has a MD5 hash in it so it's doesn't work either. I can't give Princely an absolute path to a stylesheet.

Do you have a good way to work around that?

For now, i just inlined the style in a style tag of the layout.

Is this gem still actively maintained?

Hi,

Firstly thanks for creating and maintaining the princely gem, it is heavily used by our business (https://github.com/YourTradebase) and we have benefited from it's creation.

I can see it's been a long time (2 years) since there has been any activity on this repo, are the maintainers still planning to actively maintain this gem? And is there any appetite to continue it's development?

If the current maintainers would like to pass the torch to someone else we would be happy to take over the continued maintenance of the gem as it forms an integral part of our business and would like to see the gem improved, maintained and kept compatible with new rails versions as they come out.

Please let me know your thoughts @modsognir @mbleigh @drnic

asset_file_path method broken?

Is there something wrong with asset_file_path in lib/princely/pdf_helper.rb? It seems to reference undefined variables like stylesheet and config.stylesheets_dir. This is using Princely 1.4.0, Ruby 2.0 and Rails 4.0.0.

Preserving UTF8 Encoding

I have a lot of Korean characters in my views and they seem to get lost somewhere in the process of building the PDF - any input? If I just use the standalone Princexml app the encoding is preserved...

Add ability to silent Prince fin|success message

Whenever I create a PDF (specifically when running our test suite) it outputs "fin|success" after each test that creates a PDF, making our test output much more verbose and challenging to find actual error outputs. I'd like an option to silence the princely output.

Princely hanging with 'undefined method `force_encoding' for nil:NilClass'

This might be a bit of a newbie question, and I hope I'm asking it in the right place.

Using Ubuntu 10.10, ruby 2.0.0p247, Rails 4.0.0, latest princely, Prince 9.0 rev 2

When I try to create a pdf using the below:

respond_to do |format|
  format.pdf do
    render :pdf => "preview",
      :template => "projects/preview.pdf.erb",
      :layout => "preview"
   end
end

Or by using 'make_and_send_pdf("preview")', the server hangs with this line:

PRINCE XML PDF COMMAND
/usr/bin/prince --input=html --server --log=/home/schwarzh/rails/generator/log/prince.log --silent - -o -

When I ctrl-c the rails server, I get an error of:

Completed 500 Internal Server Error in 88847ms

NoMethodError (undefined method force_encoding' for nil:NilClass): app/controllers/projects_controller.rb:111:inpreview'

Line 111 is 'render :pdf => "preview",'

I'm able to create a pdf using prince on the command line from the same HTML that I'm rendering in Rails. Am I doing something wrong or have I encountered a bug?

Thanks,
Brad

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Version 1.4

Hi, our project requirement is to use 1.4.1 version, and there I have found a bug.

in file lib/princely/pdf_helper.rb:54

Following method has issue which you guys fixed already in the latest princely version.

Issue: It should do asset.to_s.gsub(".css","")

CURRENT

  def asset_file_path(asset)
    stylesheet = stylesheet.to_s.gsub(".css","")
    File.join(config.stylesheets_dir, "#{stylesheet}.css")
  end

Need to fix this method for version 14.1. Can we change it to below code?

PROPOSED

  def asset_file_path(asset)
    asset = asset.to_s.gsub(".css","")
    File.join(config.stylesheets_dir, "#{asset}.css")
  end

Using for Rails 3.0.0 Version

Pass custom executable options

Is there a way to pass in custom executable options? I've not been able to see anywhere these can be set from reading the source code. For instance I want to be able to set the --no-network option when running in the test environment to prevent pdf generation from loading external images.

Prince supports lots of executable options, if there is currently no way to set them it would be good to have a way to be able to do this. I'm open to suggestions on the best way to implement and then I can submit a PR.

More helpful erroring

When the prince executable is not found there is no message to that effect - just a rails backtrace that mentions nil.each. It would be nice if princely pointed out "hey dummy, you don't have prince installed".

Cannot modify SafeBuffer in place

I'm trying to use Princely with a Rails 3.0.9 app. I used the instructions at tehpeh@4003fd0 to install Princely.

Right now I'm just trying to test it out. I have a route that looks like

match '/test' => 'welcome#test'

and my controller method looks like this:

class WelcomeController < ApplicationController

   ...

   def test
      make_and_send_pdf("test")
   end

And I have a file called "test.pdf.erb" in the view directory for this controller which just says 'hello world'. I'm not sure if this is the right way to set it up because I couldn't find any documentation on it but it seems to make sense.

Anyway, when I load the URL for this method, ".../test.pdf" I get the following error:

TypeError in WelcomeController#test
Cannot modify SafeBuffer in place

and the trace looks like this:

activesupport (3.0.9) lib/active_support/core_ext/string/output_safety.rb:127:in `gsub!'
princely (1.2.5) lib/princely/pdf_helper.rb:33:in `make_pdf'
princely (1.2.5) lib/princely/pdf_helper.rb:49:in `make_and_send_pdf'
app/controllers/welcome_controller.rb:8:in `test'
...

where that last line, 'welcome_controller.rb:8' is the 'make_and_send_pdf' statement above in my controller code. Does anyone know why this might be happening? I would appreciate the help. Thanks!

Undefined config

Is config defined somewhere?

https://github.com/mbleigh/princely/blob/v1.4.1/lib/princely/pdf_helper.rb#L54

I'm getting the following error on Rails2 with Ruby1.9.3.

NameError (undefined local variable or method `config' for #<DealsController:0x007fb1fee23a80>):
  princely (1.4.1) lib/princely/pdf_helper.rb:54:in `asset_file_path'
  princely (1.4.1) lib/princely/pdf_helper.rb:28:in `block in make_pdf'
  princely (1.4.1) lib/princely/pdf_helper.rb:28:in `collect'
  princely (1.4.1) lib/princely/pdf_helper.rb:28:in `make_pdf'
  princely (1.4.1) lib/princely/pdf_helper.rb:61:in `make_and_send_pdf'
  princely (1.4.1) lib/princely/pdf_helper.rb:13:in `render_with_princely'

global timeout option

We use princely to create about 10K pdfs per week. We have some external dependencies (like images) that can be quite slow. Sometimes prince will work on generating a pdf for > 30 min which is far too long.

prince has an --http-timeout option but this doesn't work because it can produce mostly empty documents. So the reasonable answer is to add a timeout block around the call to the prince executable to timeout the entire document.

I wanted to reach out to you before I went ahead and made the change to see if you have any thoughts on how you would do this, or I can just go for it.

Please feel free to PM me here and we can figure it out. Talk soon!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.