This blog post will cover how to generate the preview of pdf files using Carrierwave gem.
Carrierwave gem has an inbuilt "manipulate!" method which reads the file and loops over every page. This "manipulate!" method works as expected for a pdf having 300-400 pages but when I uploaded a pdf having 1000+ pages my machine crashed!!!
Carrierwave gem has an inbuilt "manipulate!" method which reads the file and loops over every page. This "manipulate!" method works as expected for a pdf having 300-400 pages but when I uploaded a pdf having 1000+ pages my machine crashed!!!
That is when I started debugging the code and found out that there is an issue with the manipulate method.
Here is my original code.
Here is my original code.
Above code works only when the number of pages is small but for large number of pages the machine crashes.
The implementation for manipulate! method is shown below.
Notice it will read the whole file each time we need to generate the preview of a single page and there lies our problem.
The solution: USE RMAGICK.
In the above code, it will read only the first page of the uploaded pdf and create the preview instead of processing all the pages.
current_path + "[0]" will read the first frame/page of the uploaded pdf.
The solution: USE RMAGICK.
current_path + "[0]" will read the first frame/page of the uploaded pdf.
Once we have the first page of pdf then we just need to resize it to the required height and width and save at the desired path.
This solved my problem.
awesome post presented by you..your writing style is fabulous and keep update with your blogs Ruby on Rails Online Training India
ReplyDelete