Commit d516a1fc authored by chia-lin.lin's avatar chia-lin.lin 💬
Browse files

convert to PNG from PDF

parent 1d3e7593
......@@ -7,7 +7,7 @@ gem 'rails', '~> 5.0.0'
gem 'bcrypt','~>3.1.11'
gem 'devise'
gem 'rmagick', '~> 4.1', '>= 4.1.2'
gem 'will_paginate', '~> 3.1.0'
gem 'bootstrap-will_paginate', '~> 0.0.10'
gem 'font-awesome-rails'
......
......@@ -184,6 +184,7 @@ GEM
responders (2.4.1)
actionpack (>= 4.2.0, < 6.0)
railties (>= 4.2.0, < 6.0)
rmagick (4.1.2)
rubyzip (2.0.0)
sablon (0.3.2)
nokogiri (>= 1.8.5)
......@@ -272,6 +273,7 @@ DEPENDENCIES
prawn-table
rack-mini-profiler!
rails (~> 5.0.0)
rmagick (~> 4.1, >= 4.1.2)
sablon
sass-rails (~> 5.0)
spring
......
require 'RMagick'
class SamplesController < ApplicationController
include SamplesHelper
before_action :authenticate_user!
before_action :correct_user, only: :destroy
before_action :set_sample, only: [:show, :edit, :update, :destroy,:update_status]
......@@ -85,14 +87,27 @@ class SamplesController < ApplicationController
@sample = current_user.samples.find(params[:id])
@user = @sample && @sample.user
end
respond_to do |format|
format.html
format.js
format.pdf do
pdf = SamplePdf.new(@sample,operator?,@user)
send_data pdf.render, filename: "sample_#{@sample.id}.pdf",
type:"application/pdf",
disposition: "inline"
file_type = params["format"]
if file_type == 'pdf'
respond_to do |format|
format.html
format.js
format.pdf do
pdf = SamplePdf.new(@sample,operator?,@user)
send_data pdf.render, filename: "sample_#{@sample.id}.pdf",
type:"application/pdf",
disposition: "inline"
end
end
end
if file_type == 'png'
respond_to do |format|
format.html
format.js
format.png do
pdf = SamplePdf.new(@sample,operator?,@user)
send_image(pdf)
end
end
end
end
......@@ -137,6 +152,13 @@ class SamplesController < ApplicationController
flash[:success]= "Sending e-mail to user"
end
def send_image(pdf)
pdf_file_name = "sample_#{@sample.id}.pdf"
png_file_name = "sample_#{@sample.id}.png"
result = pdf_to_png(pdf, pdf_file_name, png_file_name)
send_data result.to_blob, filename: png_file_name, type: "image/png" , disposition: "inline"
end
private
def sample_params
......
......@@ -30,4 +30,23 @@ module SamplesHelper
s.abs.round(2)
end
def pdf_to_png(pdf, pdf_file_name, png_file_name)
pdf.render_file(pdf_file_name)
images = Magick::ImageList.new(pdf_file_name) do
self.density = 400
self.quality = 100
self.format = "PNG"
end
File.delete(pdf_file_name)
images.each_with_index do |a, i|
a.format = 'PNG'
end
result = images.montage do |m|
m.tile = 1
m.geometry = "1024x1024+10+5"
m.background_color = "white"
end
result.format = "PNG"
result
end
end
class UserMailer < ApplicationMailer
include SamplesHelper
default from: 'notifications@example.com'
respond_to? :pdf
def measured_email(user,sample)
@sample = sample
@user = user
attachments["report.pdf"] =[SamplePdf.new(@sample,@user).render]
pdf = SamplePdf.new(@sample,@user)
pdf_file_name = "report.pdf"
png_file_name = "report.png"
attachments[pdf_file_name] =[pdf.render]
png = pdf_to_png(pdf, pdf_file_name, png_file_name)
attachments[png_file_name] = {:mime_type => 'image/png', :content => png.to_blob }
mail to: @user.email, subject: "Your sample has been measured", from: "elemental_analysis@ioc.kit.edu"
end
......
......@@ -142,4 +142,4 @@ $(document).ready(function(){
</div>
<%end%>
<br>
<p><%=link_to "Download PDF", sample_path(@sample, format: "pdf"), target: "_blank" %><p>
<p><%=link_to "Download PDF", sample_path(@sample, format: "pdf"), target: "_blank" %>&nbsp;<%=link_to "Download PNG", sample_path(@sample, format: "png"), target: "_blank" %><p>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment