rails 3 wicked_pdf images from html into pdf - ruby-on-rails-3

I've encountered a problem with displaying images in pdf's and html's.
Basically what I want is to make my html view be downloadable in pdf. So I've created a partial _pdf_view.html.haml, which looks (for now) like so:
%p
= wicked_pdf_image_tag "vehicles/Toyota_Echo1-1.jpg"
then I simply render this partial both in download.html.haml and in download.pdf.haml. The idea is that the html file previews what is going to be downloaded as a pdf.
The funny part is that this code will put the image nicely in my pdf, but not in my html. In PDF there needs to be specified full file path, however, in html that won't fly. If I change the path to "/vehicles/Toyota_Echo1-1.jpg" then it is the other way around...
Any way around it, besides creating separate files or separate chunks of code just for images?
P.S I'm using: rails 3.2.12, wkhtmltopdf 0.9.9, wicked_pdf 0.9.5

Change the image tag created based on the :format.
- if params[:format] == 'pdf'
= wicked_pdf_image_tag 'vehicles/Toyota_Echo1-1.jpg'
- else
= image_tag 'vehicles/Toyota_Echo1-1.jpg'

Related

mPDF missing non-local images

I am generating PDF from HTML using mPDF 5.7. The generated PDF is fine when generated locally, but on server, the images are not getting rendered completely soon enough, and hence PDF is missing all images.
Has anybody encountered this issue?
Whats the solution for this?
Yes, if the images are PNG, you need to install the php-gd extension, because mPDF needs it to render alpha maps (transparencies of the images).
The issue can be debugged by setting up a debug flag/option for your script, and adding code like
if ($debug) {
$mpdf->debug = true;
$mpdf->showImageErrors = true;
}
then you'll be able to see the actual error that caused the missing images
which is
mPDF error: IMAGE Error (https://url.to.server/image.png): GD library required for PNG image (alpha channel)
(actually, there will be square icons with an X, like in old InternetExplorer "missing image" style).
You can add GD extension to composer.json, see this answer

Generation pdf truncated

I have to transform a HTML badly built in PDF.
I transformed the HTML file into XTML with the class Tidy.
Then, generated my PDF with XMLWorkerHelper.
It's work but the generated PDF is not correct.
The images are missing and the text is truncated on certain files.
What specific configuration may I use to solve this problem?
It is the first time when I use these class and it's not easy.
Thanks for your help
I have files html badly constituted to transform into PDF.
I thus used at first Tidy to format them in XHTML and then XMLWorkerHelper to generate the pdf.
I've used itextpdf-5.4.2 xmlworker-5.4.2 .
PdfWriter writer = PdfWriter.getInstance(documentPDF, new FileOutputStream(pdfFilename));
documentPDF.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, documentPDF, new FileInputStream(HTMLFileName));
I can't post my file, it's too big.

Find filename of view template in Rails 3 based on action and format

I'm upgrading an old Rails 2.3 (I know, I know) which uses an external program to modify a PDF before rendering it for an action. Therefore, it's necessary for the action to be able to determine the filename of the PDF source file, which is stored in the view template directory, which was done with:
view_paths.find_template(default_template_name(:show), :pdf).filename
However, this no longer works in Rails 3. I've tried something like:
lookup_context.find :show, controller_name
But I can't find a way to specify the format, so that always returns the path for the HTML template (app/views/name/show.html.erb). How can I get the filename of the template for the PDF format (app/views/name/show.pdf)?
Though more complicated than I would like, I eventually found this, which seems to work:
ActionView::LookupContext.
new(view_paths, {formats: :pdf}).
find(:show, controller_name).
identifier

Images not showing in pdf

i have a problem i am deveoping a application using rails 2.3.8 . my problem is with the pdf. when a generated the pdf it shows all images in Linux.(ubuntu)
but the same code when i tried in windows7 the pdf is not showing the images in the body.the header and footer images are loading. the header and footer in the layout. i am using wickedpdf for pdf generation.
my code is
<%= wicked_pdf_image_tag "#{Rails.root}/public/images/master_student/profile/default_student.png" ,:width=>85,:height=>100 %>
the code working fine in the ubuntu but not working in the windows7..
please help
I just overcame a similar issue on Windows with images uploaded through Paperclip, because WickedPDF's wicked_pdf_image_tag helper requires the images to be in public/images.
I used the solution given here for that problem, but I still couldn't get the images to render in the PDF. Then I discovered that if I put the image into the public/images folder and referenced it that way, the wicked_pdf_image_tag helper rendered the image tag with forward slashes instead of backslashes after the 'file://'.
This is the helper method I ended up with, which replaces wicked_pdf_image_tag:
module ApplicationHelper
def wicked_pdf_image_tag_for_public(img, options={})
if img[0] == "/"
# Remove the leading slash
new_image = img.slice(1..-1)
image_tag "file:///#{Rails.root.join('public', new_image).to_s.gsub("/", "\\")}"
else
image_tag "file://#{Rails.root.join('public', 'images', img).to_s.gsub("/", "\\")}}", options
end
end
end

HAML markdown rendering and showing ERB within

I have this in one of my HAML templates:
:markdown
#{render 'home.md'}
and in home.md I have:
There are **#{#photo_count}** photos.
When viewing the site, it literally outputs that. How can I get the #photo_count variable to be interpolated?
For a pure Markdown file, I don't think you'll be able to do what you want as the format itself won't support your Ruby variable.
If you don't mind changing your markdown file to a HAML partial file (no need to change its content), you could do something like this (I've used something similar to the code below using the RDiscount gem; your mileage may vary with other Markdown gems...):
app/controllers/pages_controller.rb
def home
#photo_count = 10
end
app/views/pages/home.html.haml
:markdown
#{render 'home_page'}
app/views/pages/_home_page.html.haml
There are **#{#photo_count}** photos.
See also this StackOverflow Q&A for other ideas:
How can I automatically render partials using markdown in Rails 3?