Module: MapFish::Print::Controller

Defined in:
lib/print.rb

Overview

To have a controller for the print module, just mix-in this module to one of your controllers. This controller must have an attribute @configFile. For example: class PrintController < ApplicationController

include MapFish::Print::Controller

def initialize()
    @configFile = "/home/toto/rails/Test/vendor/java/print/print-standalone/samples/config.yaml"
end

end

Constant Summary collapse

TEMP_PREFIX =
Dir::tmpdir+"/mfPrintTempFile"
TEMP_SUFFIX =
".pdf"
TEMP_PURGE_SECONDS =
600

Instance Method Summary collapse

Instance Method Details

#createObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/print.rb', line 75

def create
    cleanupTempFiles

    tempId = rand(2**31) #TODO: not secure enough
    temp = TEMP_PREFIX + tempId.to_s + TEMP_SUFFIX
    cmd = baseCmd() + " --output=" + temp
    result = ""
    errors = ""
    status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
        body = request.body
        FileUtils.copy_stream(body, stdin)
        body.close()
        stdin.close()
        result = stdout.readlines().join("\n")
        errors = stderr.readlines().join("\n")
    end
    if status.nil? || status.exitstatus != 0
        raise JavaError.new(cmd, errors)
    else
        respond_to do |format|
            format.json do
                render :json=>{ 'getURL' => url_for(:action=>'show', :id=>tempId)+".pdf" }
            end
        end
    end
end

#infoObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/print.rb', line 49

def info
    cmd = baseCmd() + " --clientConfig"
    result = ""
    errors = ""
    status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|

        result = stdout.readlines().join("\n")
        errors = stderr.readlines().join("\n")
    end
    if status.nil? || status.exitstatus != 0
        raise JavaError.new(cmd, errors)
    else
        info = ActiveSupport::JSON.decode(result)
        info['createURL']=url_for(:action=>'create')+'.json'
        respond_to do |format|
            format.json do
                if params[:var]
                    render :text=>"var "+params[:var]+"="+result+";"
                else
                    render :json=>info
                end
            end
        end
    end
end

#showObject



102
103
104
105
106
107
108
109
# File 'lib/print.rb', line 102

def show
    temp = TEMP_PREFIX + params[:id] + TEMP_SUFFIX
    respond_to do |format|
        format.pdf do
            send_file temp, :type=>'application/x-pdf', :disposition=>'attachment', :filename=>params[:id]+'.pdf'
        end
    end
end