How to speed up generating pdf by Wicked gem in Rails - ruby

Services file:
pdf = WickedPdf.new.pdf_from_string(
ActionController::Base.new.render_to_string(
'ticket_managements/_render_pdf.html.erb',
template: "ticket_managements/_render_pdf.html.erb",
locals: { :ticket_order => ticket_order }
),
)
When I try to generate over 30 pdf files, it takes 3 to 5 minutes. It's so bad and makes users mad
I also try to some other gem but as I researched and try with Benchmark, Wicked still is faster
Or try to add dpi option, it stays that way
Any idea or solution for this issue?
Thanks in advance

Related

Check if a PDF file is corrupted with Ruby?

Ideally I want to avoid using gems, as I am currently using: pdf-reader, combine-pdf and origami
Each gem if it comes across a damaged pdf sometimes doesn't send an exception but stays there and does nothing.
So I would like you to please help me with a code to see if the file is damaged or not.
I have noticed that some pdf files do not have hexadecimal (hex 25 50 44 46 | %PDF) but I'm afraid it's not a universal solution.
Besides all those gems sometimes throw exceptions even when the pdf does work, but at least if I'm sure the pdf works, I'll know what to do.
I could start there. How do I read hexadecimal with ruby? Is the only way to check a pdf?
I've hit this situation before when verifying grub bootloaders with ruby. I found the easiest solution was to do a pre-check of the hex that I know is supposed to exist. Something along the lines of this:
result = `hexdump pdf_file.pdf | head -n 1`
valid_pdf = result.split(" ")[1..2] == ["2550", "4446"]
Over time you can expand your check to look for other bad pdfs ahead of time.
One good practice to circumvent the locks on your pdf hangs, is to use the Timeout functionality in ruby This way you can properly exit and not have to forcefully shutdown your program.
result = IO.binread('file.pdf', 4).unpack("H*").first
valid_pdf = result == '25504446'
Will do the comparison for those first 4 bytes
File.read(pdf_filepath, 4) == "%PDF"

Feedjira: Feed update returns duplicates

I am trying to print rss/atom feed updates using ruby. Feedjira seems to be the best bet for this. Unfortunately the update feature does not seem to work properly. I get duplicate entries.
Here is a simple example that produces the problem:
require 'feedjira'
require 'pp'
feed = Feedjira::Feed.fetch_and_parse "http://lorem-rss.herokuapp.com/feed?unit=second&interval=10"
loop do
feed = Feedjira::Feed.update(feed)
pp feed.new_entries
sleep 20
end
Any suggestions? Maybe other libraries? Or am I missing something important when using Feedjira?
There a several questions around this topic for Feedzirra the former name for Feedjira, but the update feature seems to be a new feature: http://feedjira.com/updating-feeds.html
The updated functionality was removed from feedjira due to serious problems. See
(commit) https://github.com/feedjira/feedjira/commit/6f56516934a9bdb8691f2bbe98be0f2b7c25b7ea
(discussion) https://github.com/feedjira/feedjira/issues/218

Trouble with greasemonkey script to refresh page

I'm downloading data from an online database using the imacros plugin for firefox. It works perfectly, except when the page times out. I've moved to downloading very small chunks of data since then the website times out much less frequently, but it still times out occasionally.
The problem is there's no way to code if statements directly into the imacros macro, so I wanted to have a greasemonkey script running along side it that will refresh the page if it timesout. The website only times out in one place for me, and when it does it shows the following:
can't open file
I never learned Java or Javascript and all of my programming experience deals with numerical methods, but I've been been piecing together code and adapting it to try and solve my problem.
The following is my base code:
var RefreshTime = '20';
var str1 = document.body.innerHTML
var str2 = "can't open file"
if (str1.search(str2) > 0) {
if (StRefTime > 0) setTimeout("location.reload(true);",RefreshTime*1000);
}
Since the browser I'm running this in is currently being used exclusively for this script I just have #include *
Basically what this is meant to do is refresh the page every 20 seconds as long "can't open file" is found the webpage. This works perfectly on other websites when I test it, but doesn't work at all on the website I need it to. It might be worth noting that when I refresh, firefox asks me to confirm resending post data - but imacros does have a function that I tested and works on this site to automatically confirm whenever the dialog is displayed.
After doing some reading I found that other people had similar problems, and I think the root of the cause is that if the page is arrived at through AJAX then greasemonkey misses it.
To address this I tried to implement the suggested solutions to those people, but I've been unsuccessful. For example I've tried putting the code I listed above into the solution given here: Run Greasemonkey script on the same page, multiple times? , for example
var RefreshTime = '20';
var str1 = document.body.innerHTML
var str2 = "can't open file"
function highlightGoodComments (jNode) {
//***** YOUR CODE HERE *****
if (/str2/i.test (jNode.text () ) ) {
if (str1.search(str2) > 0) {
if (RefreshTime > 0) setTimeout("location.reload(true);",RefreshTime*1000);
}
}
But this doesn't work either, and I've tried a couple variations of it (for example getting rid of str2 and just putting "open file" and things like that) and I've also tried and failed to make the following work https://gist.github.com/BrockA/2625891. I also tried wrapping all of my initial code in a setInterval loop with the hope that it would just check constantly for "can't open file" even without any page loading, but that didn't work either.
Unfortunately the website I'm running my script on is hidden behind a paywall, and it's hard to test my code since it times out somewhat randomly so there's no way to check if my code works outside of just waiting a long time and seeing if it failed. I know nothing about AJAX, so I'm not sure how to confirm that that is what's happening, but I still believe it to be so since my problem seemed similar to other people's - and when I enable this macro it does seem to interfere with other parts of the website but not the parts that my script works on.
I was hoping somebody could help me out, since I'm pretty stuck here and already in over my head. Even if you can't help, having some sort of AJAX website where I could test code without waiting several hours would be helpful to me trying to figure this out myself, if anybody knows of one. Thank you for reading through this, I really do appreciate any help anybody can offer.
The question is very long so I didn't bother to really read it. Since I don't use GreasMonkey I can tell you the iMacros solution.
Check if html element exists with iMacros and javascript
Here is a model how to built JS iMacros script and include if clause. You can make a testing macro that tests is t here "can't open" web page.
Also you can try to use
SET !TIMEOUT_PAGE 120
command to increase the time of page loading to 120 seconds. Try some of these if it can help.

Multiple Converters/Generators in Jekyll?

I would like to write a Jekyll plugin that makes all posts available in PDF format by utilizing Kramdown's LaTeX export capabilities. For each post in Markdown format, I'd like to end up with the normal .html post along with a .tex file containing the LaTeX markup and finally a .pdf.
Following the documentation for creating plugins, I see two ways of approaching the problem, either with a Converter or with a Generator.
Converter plugins seem to run after the built-in Converters, so the .markdown files have all been converted to .html by the time they reach the Converter.
When I try to implement a Generator, I am able to use fileutils to write a file successfully, but by the end of Jekyll's cycle, that file has been removed. It seems there's a StaticFile class which you can use to register new output files with Jekyll, but I cannot find any real guidance on how to use it.
If you take a look at the ThumbGenerator class in this: https://github.com/matthewowen/jekyll-slideshow/blob/master/_plugins/jekyll_slideshow.rb you'll seen a similar example. This particular plugin makes thumbnail sized versions of all images in the site. Hopefully it gives a useful guide to how you can interact with Jekyll's StaticFile class (though I'm not a Ruby pro, so forgive any poor style).
Unfortunately, there isn't really documentation for this - I gleaned it from reading through the source.
I wrote this a few months ago and don't particularly remember the details (which is why I gave an example rather than a workthrough), but if this doesn't get you on the right track let me know and I'll try to help.
I try to do the same but with direct html->pdf conversion.
It did not work inside a gitlab-ci pipeline at this time, nonetheless it work on my workstation (see here) with a third possibility : a hook !
(here with pdfkit)
require 'pdfkit'
module Jekyll
Jekyll::Hooks.register :site, :post_write do |post|
post.posts.docs.each do |post|
filename = post.site.dest + post.id + ".pdf"
dirname = File.dirname(filename)
Dir.mkdir(dirname) unless File.exists?(dirname)
kit = PDFKit.new(post.content, :page_size => 'Letter')
kit.stylesheets << './css/bootstrap.min.css'
kit.to_file(filename)
end
end
end

Adding Bootstrap Less to Sinatra

I have a Modular Sinatra app and I'm trying to add the Bootstrap less to the application.
get '/bootstrap/application.css' do
less :"bootstrap/bootstrap"
end
I have all less files in views/bootstrap, including bootstrap.less.
I get this error:
Less::ParseError at /bootstrap/application.css 'reset.less' wasn't found.
The first real line of Bootstrap.less is:
// CSS Reset
#import "reset.less";
I've tried all different path formats, but it never finds the files it's looking to import. Any idea how to make this work?
Have you tried passing the :paths config option?
get '/bootstrap/application.css' do
less :"bootstrap/bootstrap", :paths => ["views/bootstrap"]
end
I've had problems with this option in the past but it may work for you.
I found a solution that worked for me here: Parsing LESS options in a Sinatra app
Since this question was the one I found first on Google, I thought I'd leave the link here so that others can find it.

Resources