Class: DynamicPDFApi::PdfImage
- Defined in:
- lib/ruby_client/Imaging/PdfImage.rb
Overview
Represents a PDF Rasterizing endpoint that converts PDF to image.
Instance Attribute Summary collapse
-
#image_format ⇒ Object
Gets or sets the image format for rasterization.
-
#image_size ⇒ Object
Gets or sets the size of the rasterized images.
-
#page_count ⇒ Object
Gets or sets the number of pages to rasterize.
-
#resource ⇒ Object
readonly
Represents a PdfResource for converting PDF pages to images.
-
#start_page_number ⇒ Object
Gets or sets the starting page number for rasterization.
Attributes inherited from Endpoint
#_endpoint_name, #api_key, #base_url
Instance Method Summary collapse
-
#initialize(resource) ⇒ PdfImage
constructor
A new instance of PdfImage.
- #process ⇒ Object
- #to_json(_options = {}) ⇒ Object
Methods inherited from Endpoint
Constructor Details
#initialize(resource) ⇒ PdfImage
Returns a new instance of PdfImage.
44 45 46 47 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 44 def initialize(resource) @resource = resource @_endpoint_name = 'pdf-image' end |
Instance Attribute Details
#image_format ⇒ Object
Gets or sets the image format for rasterization.
37 38 39 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 37 def image_format @image_format end |
#image_size ⇒ Object
Gets or sets the size of the rasterized images.
42 43 44 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 42 def image_size @image_size end |
#page_count ⇒ Object
Gets or sets the number of pages to rasterize.
32 33 34 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 32 def page_count @page_count end |
#resource ⇒ Object (readonly)
Represents a PdfResource for converting PDF pages to images.
22 23 24 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 22 def resource @resource end |
#start_page_number ⇒ Object
Gets or sets the starting page number for rasterization.
27 28 29 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 27 def start_page_number @start_page_number end |
Instance Method Details
#process ⇒ Object
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 74 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 49 def process header = { 'authorization': "Bearer #{@api_key}", 'Expect': "100-continue", } uri = URI.parse("#{@base_url}/v1.0/#{@_endpoint_name}") params = {} if @start_page_number!=nil params['sp'] = @start_page_number params['pc'] = @page_count end # Adding ImageSize parameters if @image_size is present if @image_size case @image_size when DpiImageSize params['is'] = @image_size.type.to_s params['hd'] = @image_size.horizontal_dpi.to_s if @image_size.horizontal_dpi params['vd'] = @image_size.vertical_dpi.to_s if @image_size.vertical_dpi when FixedImageSize params['is'] = @image_size.type.to_s params['ht'] = @image_size.height.to_s if @image_size.height params['wd'] = @image_size.width.to_s if @image_size.width params['ut'] = @image_size.unit.to_s if @image_size.unit when MaxImageSize params['is'] = @image_size.type.to_s params['mh'] = @image_size.max_height.to_s if @image_size.max_height params['mw'] = @image_size.max_width.to_s if @image_size.max_width params['ut'] = @image_size.unit.to_s if @image_size.unit when PercentageImageSize params['is'] = @image_size.type.to_s params['hp'] = @image_size.horizontal_percentage.to_s if @image_size.horizontal_percentage params['vp'] = @image_size.vertical_percentage.to_s if @image_size.vertical_percentage end end # Adding ImageFormat parameters if @image_format is present if @image_format case @image_format when GifImageFormat params['if'] = @image_format.type.to_s params['dp'] = @image_format.dithering_percent.to_s if @image_format.dithering_percent params['da'] = @image_format.dithering_algorithm.to_s if @image_format.dithering_algorithm when JpegImageFormat params['if'] = @image_format.type.to_s params['qt'] = @image_format.quality.to_s if @image_format.quality when PngImageFormat params['if'] = @image_format.type.to_s if @image_format.color_format params['cf'] = @image_format.color_format.type.to_s case @image_format.color_format when PngIndexedColorFormat params['da'] = @image_format.color_format.dithering_algorithm.to_s if @image_format.color_format.dithering_algorithm params['dp'] = @image_format.color_format.dithering_percent.to_s if @image_format.color_format.dithering_percent params['qa'] = @image_format.color_format.quantization_algorithm.to_s if @image_format.color_format.quantization_algorithm when PngMonochromeColorFormat params['bt'] = @image_format.color_format.black_threshold.to_s if @image_format.color_format.black_threshold params['da'] = @image_format.color_format.dithering_algorithm.to_s if @image_format.color_format.dithering_algorithm params['dp'] = @image_format.color_format.dithering_percent.to_s if @image_format.color_format.dithering_percent end end when TiffImageFormat params['if'] = @image_format.type.to_s params['mp'] = 'true' if @image_format.multi_page if @image_format.color_format params['cf'] = @image_format.color_format.type.to_s case @image_format.color_format when TiffIndexedColorFormat params['da'] = @image_format.color_format.dithering_algorithm.to_s if @image_format.color_format.dithering_algorithm params['dp'] = @image_format.color_format.dithering_percent.to_s if @image_format.color_format.dithering_percent params['qa'] = @image_format.color_format.quantization_algorithm.to_s if @image_format.color_format.quantization_algorithm when TiffMonochromeColorFormat params['ct'] = @image_format.color_format.compression_type.to_s if @image_format.color_format.compression_type params['bt'] = @image_format.color_format.black_threshold.to_s if @image_format.color_format.black_threshold params['da'] = @image_format.color_format.dithering_algorithm.to_s if @image_format.color_format.dithering_algorithm params['dp'] = @image_format.color_format.dithering_percent.to_s if @image_format.color_format.dithering_percent end end when BmpImageFormat params['if'] = @image_format.type.to_s if @image_format.color_format params['cf'] = @image_format.color_format.type.to_s case @image_format.color_format when BmpMonochromeColorFormat params['bt'] = @image_format.color_format.black_threshold.to_s if @image_format.color_format.black_threshold params['dp'] = @image_format.color_format.dithering_percent.to_s if @image_format.color_format.dithering_percent params['da'] = @image_format.color_format.dithering_algorithm.to_s if @image_format.color_format.dithering_algorithm end end end end uri.query = URI.encode_www_form(params) request = Net::HTTP::Post.new(uri.request_uri, header) request.set_form_data(params) = { use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE } resource_array = [] resource_array << ['pdf', @resource.data, { content_type: @resource._mime_type, filename: @resource.resource_name }] if !@resource.nil? request.set_form(resource_array, "multipart/form-data") response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end rasterizer_response = PdfImageResponse.new if @resource == nil raise 'Required a PDF Resource.' end rasterizer_response.status_code = response.code out_data = response.body if rasterizer_response.status_code == "200" pdf_image = JSON.parse(out_data) image_type = pdf_image['contentType'] rasterizer_response.image_format = image_type.split('/')[1] rasterizer_response.content_type = image_type rasterizer_response.horizontal_dpi = pdf_image['horizontalDpi'] rasterizer_response.vertical_dpi = pdf_image['verticalDpi'] pdf_image['images'].each do |img| image = Image.new image.page_number = img['pageNumber'] || 0 image.data = img['data'] || '' image.billed_pages = img['billedPages'] || 0 image.width = img['width'] || 0 image.height = img['height'] || 0 rasterizer_response.images << image end rasterizer_response.status_code = response.code rasterizer_response.is_successful = true else rasterizer_response.error_json = out_data rasterizer_response. = if !out_data_json["message"].nil? out_data_json["message"] else "status_code : #{Net::HTTPResponse::CODE_TO_OBJ[rasterizer_response.status_code]}" end rasterizer_response.error_id = out_data_json["id"] rasterizer_response.is_successful = false end rasterizer_response end |
#to_json(_options = {}) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/ruby_client/Imaging/PdfImage.rb', line 200 def to_json( = {}) json_array = {} json_array["startPageNumber"] = @start_page_number unless @start_page_number.nil? json_array["pageCount"] = @page_count unless @page_count.nil? json_array["imageFormat"] = @image_format unless @image_format.nil? json_array["imageSize"] = @image_size unless @image_size.nil? JSON.pretty_generate(json_array) end |