Class: Pdfcrowd::PdfToPdfClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfcrowd.rb

Overview

Conversion from PDF to PDF.

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ PdfToPdfClient

Constructor for the Pdfcrowd API client.

  • user_name - Your username at Pdfcrowd.

  • api_key - Your API key.



2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
# File 'lib/pdfcrowd.rb', line 2755

def initialize(user_name, api_key)
    @helper = ConnectionHelper.new(user_name, api_key)
    @fields = {
        'input_format'=>'pdf',
        'output_format'=>'pdf'
    }
    @file_id = 1
    @files = {}
    @raw_data = {}
end

Instance Method Details

#addPdfFile(file_path) ⇒ Object

Add a PDF file to the list of the input PDFs.

  • file_path - The file path to a local PDF file. The file must exist and not be empty.

  • Returns - The converter object.



2809
2810
2811
2812
2813
2814
2815
2816
2817
# File 'lib/pdfcrowd.rb', line 2809

def addPdfFile(file_path)
    if (!(File.file?(file_path) && !File.zero?(file_path)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "pdf-to-pdf", "The file must exist and not be empty.", "add_pdf_file"), 470);
    end
    
    @files['f_%s' % @file_id] = file_path
    @file_id += 1
    self
end

#addPdfRawData(pdf_raw_data) ⇒ Object

Add in-memory raw PDF data to the list of the input PDFs.Typical usage is for adding PDF created by another Pdfcrowd converter. Example in PHP: $clientPdf2Pdf->addPdfRawData($clientHtml2Pdf->convertUrl(‘www.example.com’));

  • pdf_raw_data - The raw PDF data. The input data must be PDF content.

  • Returns - The converter object.



2823
2824
2825
2826
2827
2828
2829
2830
2831
# File 'lib/pdfcrowd.rb', line 2823

def addPdfRawData(pdf_raw_data)
    if (!(!pdf_raw_data.nil? && pdf_raw_data.length > 300 and pdf_raw_data[0...4] == '%PDF'))
        raise Error.new(Pdfcrowd.create_invalid_value_message("raw PDF data", "pdf_raw_data", "pdf-to-pdf", "The input data must be PDF content.", "add_pdf_raw_data"), 470);
    end
    
    @raw_data['f_%s' % @file_id] = pdf_raw_data
    @file_id += 1
    self
end

#convertObject

Perform an action on the input files.

  • Returns - Byte array containing the output PDF.



2781
2782
2783
# File 'lib/pdfcrowd.rb', line 2781

def convert()
    @helper.post(@fields, @files, @raw_data)
end

#convertToFile(file_path) ⇒ Object

Perform an action on the input files and write the output PDF to a file.

  • file_path - The output file path. The string must not be empty.



2795
2796
2797
2798
2799
2800
2801
2802
2803
# File 'lib/pdfcrowd.rb', line 2795

def convertToFile(file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "pdf-to-pdf", "The string must not be empty.", "convert_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    convertToStream(output_file)
    output_file.close()
end

#convertToStream(out_stream) ⇒ Object

Perform an action on the input files and write the output PDF to an output stream.

  • out_stream - The output stream that will contain the output PDF.



2788
2789
2790
# File 'lib/pdfcrowd.rb', line 2788

def convertToStream(out_stream)
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#getConsumedCreditCountObject

Get the number of credits consumed by the last conversion.

  • Returns - The number of credits.



2858
2859
2860
# File 'lib/pdfcrowd.rb', line 2858

def getConsumedCreditCount()
    return @helper.getConsumedCreditCount()
end

#getDebugLogUrlObject

Get the URL of the debug log for the last conversion.

  • Returns - The link to the debug log.



2844
2845
2846
# File 'lib/pdfcrowd.rb', line 2844

def getDebugLogUrl()
    return @helper.getDebugLogUrl()
end

#getJobIdObject

Get the job id.

  • Returns - The unique job identifier.



2864
2865
2866
# File 'lib/pdfcrowd.rb', line 2864

def getJobId()
    return @helper.getJobId()
end

#getOutputSizeObject

Get the size of the output in bytes.

  • Returns - The count of bytes.



2876
2877
2878
# File 'lib/pdfcrowd.rb', line 2876

def getOutputSize()
    return @helper.getOutputSize()
end

#getPageCountObject

Get the total number of pages in the output document.

  • Returns - The page count.



2870
2871
2872
# File 'lib/pdfcrowd.rb', line 2870

def getPageCount()
    return @helper.getPageCount()
end

#getRemainingCreditCountObject

Get the number of conversion credits available in your account. The returned value can differ from the actual count if you run parallel conversions. The special value 999999 is returned if the information is not available.

  • Returns - The number of credits.



2852
2853
2854
# File 'lib/pdfcrowd.rb', line 2852

def getRemainingCreditCount()
    return @helper.getRemainingCreditCount()
end

#setAction(action) ⇒ Object

Specifies the action to be performed on the input PDFs.

  • action - Allowed values are join, shuffle.

  • Returns - The converter object.



2770
2771
2772
2773
2774
2775
2776
2777
# File 'lib/pdfcrowd.rb', line 2770

def setAction(action)
    unless /(?i)^(join|shuffle)$/.match(action)
        raise Error.new(Pdfcrowd.create_invalid_value_message(action, "action", "pdf-to-pdf", "Allowed values are join, shuffle.", "set_action"), 470);
    end
    
    @fields['action'] = action
    self
end

#setDebugLog(debug_log) ⇒ Object

Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log can be obtained from the getDebugLogUrl method or available in conversion statistics.

  • debug_log - Set to true to enable the debug logging.

  • Returns - The converter object.



2837
2838
2839
2840
# File 'lib/pdfcrowd.rb', line 2837

def setDebugLog(debug_log)
    @fields['debug_log'] = debug_log
    self
end

#setProxy(host, port, user_name, password) ⇒ Object

Specifies an HTTP proxy that the API client library will use to connect to the internet.

  • host - The proxy hostname.

  • port - The proxy port.

  • user_name - The username.

  • password - The password.

  • Returns - The converter object.



2914
2915
2916
2917
# File 'lib/pdfcrowd.rb', line 2914

def setProxy(host, port, user_name, password)
    @helper.setProxy(host, port, user_name, password)
    self
end

#setRetryCount(retry_count) ⇒ Object

Specifies the number of retries when the 502 HTTP status code is received. The 502 status code indicates a temporary network issue. This feature can be disabled by setting to 0.

  • retry_count - Number of retries wanted.

  • Returns - The converter object.



2923
2924
2925
2926
# File 'lib/pdfcrowd.rb', line 2923

def setRetryCount(retry_count)
    @helper.setRetryCount(retry_count)
    self
end

#setTag(tag) ⇒ Object

Tag the conversion with a custom value. The tag is used in conversion statistics. A value longer than 32 characters is cut off.

  • tag - A string with the custom tag.

  • Returns - The converter object.



2884
2885
2886
2887
# File 'lib/pdfcrowd.rb', line 2884

def setTag(tag)
    @fields['tag'] = tag
    self
end

#setUseHttp(use_http) ⇒ Object

Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.

  • use_http - Set to true to use HTTP.

  • Returns - The converter object.



2893
2894
2895
2896
# File 'lib/pdfcrowd.rb', line 2893

def setUseHttp(use_http)
    @helper.setUseHttp(use_http)
    self
end

#setUserAgent(user_agent) ⇒ Object

Set a custom user agent HTTP header. It can be usefull if you are behind some proxy or firewall.

  • user_agent - The user agent string.

  • Returns - The converter object.



2902
2903
2904
2905
# File 'lib/pdfcrowd.rb', line 2902

def setUserAgent(user_agent)
    @helper.setUserAgent(user_agent)
    self
end