Sha256: f5369248984f2ebd6a950e571ece6cdbc791defcab996123dde21b19823d1e01

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

require 'open3'
require 'fileutils'
require_relative '../html2pdf'

module Html2Pdf
  module Utils
    class << self
      # Batch convert to pdf using `wkhtmltopdf` tool
      #
      # @param [Array<String>] files the input file list
      # @param [String] base_dir the base directory
      def to_pdfs(files)
        files.each_with_index do |file, index|
          puts "Convert file #{index + 1} of #{files.size} : #{file}"
          to_pdf(file)
        end
      end

      # Convert '*.xhtml' or '*.html' to pdf
      #
      # @param filename input filename
      def to_pdf(filename)
        fail "Invalid input file #{filename}" unless File.exist?(filename)
        command = [
          'wkhtmltopdf',
          '--margin-top 4',
          '--margin-bottom 4',
          '--margin-left 4',
          '--margin-right 4',
          '--header-center "[webpage] :: [page]/[topage]"',
          '--header-spacing 1',
          '--header-font-size 8',
          '--header-line',
          '--footer-spacing 1',
          '--footer-font-size 8',
          '--footer-line',
          "#{filename}",
          "#{filename}.pdf",
          '> /dev/null']
        _stdin, _stderr, status = Open3.capture3(command.join(' '))
        fail "Problem processing #{filename}" unless status.success?
      end

      # Check and verify that the proper softwares are available.
      #
      def softwares_installed?
        AgileUtils::Helper.which('wkhtmltopdf') && AgileUtils::Helper.which('gs')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
html2pdf-0.0.5 lib/html2pdf/utils.rb
html2pdf-0.0.4 lib/html2pdf/utils.rb
html2pdf-0.0.3 lib/html2pdf/utils.rb