A Ruby client library for interacting with the Nutrient DWS Processor API. This gem provides a clean, idiomatic Ruby interface for document processing operations including conversion, OCR, watermarking, and PDF editing.
Add this line to your application's Gemfile:
gem 'nutrient-dws'And then execute:
bundle installOr install it yourself as:
gem install nutrient-dws- Ruby 2.7 or higher
- A Nutrient DWS API key (get one at Nutrient Dashboard)
require 'nutrient_dws/processor'
# Initialize the client with your API key
client = NutrientDWS::Processor::Client.new(api_key: 'YOUR_NUTRIENT_API_KEY')
# Convert a DOCX file to PDF
pdf_data = client.convert(file: 'document.docx', to: 'pdf')
File.binwrite('output.pdf', pdf_data)You can set your API key using an environment variable:
export NUTRIENT_API_KEY=your_api_key_hereThen initialize the client without passing the key explicitly:
client = NutrientDWS::Processor::Client.new(api_key: ENV['NUTRIENT_API_KEY'])Convert documents between different formats:
# Convert DOCX to PDF
pdf_data = client.convert(file: 'document.docx', to: 'pdf')
File.binwrite('output.pdf', pdf_data)
# Convert PDF to PNG
png_data = client.convert(file: 'document.pdf', to: 'png')
File.binwrite('output.png', png_data)
# Using a File object
File.open('document.docx', 'rb') do |file|
pdf_data = client.convert(file: file, to: 'pdf')
File.binwrite('output.pdf', pdf_data)
end
# Using a remote URL
pdf_data = client.convert(file: 'https://example.com/document.docx', to: 'pdf')
File.binwrite('output.pdf', pdf_data)Make scanned documents searchable:
# Perform OCR on an image (default language: English)
searchable_pdf = client.ocr(file: 'scanned_document.png')
File.binwrite('searchable.pdf', searchable_pdf)
# Specify a different language
searchable_pdf = client.ocr(file: 'document_francais.png', language: 'fra')
File.binwrite('searchable.pdf', searchable_pdf)Add text or image watermarks to PDFs:
# Add a text watermark
watermarked_pdf = client.watermark(file: 'document.pdf', text: 'CONFIDENTIAL')
File.binwrite('watermarked.pdf', watermarked_pdf)
# Add text watermark with styling options
watermarked_pdf = client.watermark(
file: 'document.pdf',
text: 'DRAFT',
font_size: 72,
opacity: 0.5,
rotation: 45
)
File.binwrite('watermarked.pdf', watermarked_pdf)
# Add an image watermark
watermarked_pdf = client.watermark(file: 'document.pdf', image: 'logo.png')
File.binwrite('watermarked.pdf', watermarked_pdf)Combine multiple documents into a single PDF:
# Merge multiple files
merged_pdf = client.merge(files: ['doc1.pdf', 'doc2.docx', 'image.png'])
File.binwrite('merged.pdf', merged_pdf)
# Files are merged in the order provided
merged_pdf = client.merge(files: ['cover.pdf', 'content.docx', 'appendix.pdf'])
File.binwrite('complete_document.pdf', merged_pdf)Extract specific pages from a PDF:
# Extract first page only
first_page = client.split(file: 'document.pdf', ranges: '1')
File.binwrite('first_page.pdf', first_page)
# Extract multiple pages and ranges
selected_pages = client.split(file: 'document.pdf', ranges: '1,3-5,10')
File.binwrite('selected_pages.pdf', selected_pages)Remove sensitive information from PDFs:
# Redact specific text
redacted_pdf = client.redact(
file: 'sensitive_document.pdf',
text: ['Social Security', 'confidential', 'secret']
)
File.binwrite('redacted.pdf', redacted_pdf)The library provides specific exception classes for different types of errors:
begin
pdf_data = client.convert(file: 'document.docx', to: 'pdf')
rescue NutrientDWS::AuthenticationError => e
puts "Authentication failed: #{e.message}"
puts "Please check your API key"
rescue NutrientDWS::APIError => e
puts "API error occurred: #{e.message}"
puts "HTTP Status: #{e.status_code}"
puts "Response Body: #{e.response_body}"
rescue Errno::ENOENT => e
puts "File not found: #{e.message}"
endNutrientDWS::Error: Base class for all library-specific errorsNutrientDWS::AuthenticationError: Raised for 401 Unauthorized responsesNutrientDWS::APIError: Raised for other non-2xx HTTP responses, provides access tostatus_codeandresponse_body
- Microsoft Office (DOCX, XLSX, PPTX)
- Images (PNG, JPEG, TIFF)
- HTML
- And many more...
- Images (PNG, JPEG, TIFF)
- Microsoft Office formats
- And many more...
For a complete list of supported formats, see the Nutrient API documentation.
After checking out the repo, run bundle install to install dependencies.
The test suite includes both unit tests and integration tests. Integration tests require a valid API key.
-
Copy the environment file and add your API key:
cp .env.example .env # Edit .env and add your NUTRIENT_API_KEY -
Add test fixtures:
# Add sample files to spec/fixtures/ # - sample.pdf # - sample.docx # - sample.png
-
Run the tests:
bundle exec rspec
To run only unit tests (without API key):
bundle exec rspec --tag ~integrationTo run the linter:
bundle exec rubocopBug reports and pull requests are welcome on GitHub at https://github.com/nutrient-io/nutrient-dws-ruby.
The gem is available as open source under the terms of the MIT License.