From abab3f9688350ecd2893d98e02785593f280cdd6 Mon Sep 17 00:00:00 2001 From: DocuSeal Date: Mon, 23 Oct 2023 22:48:52 +0300 Subject: [PATCH] do not use libvips pdf on large files --- Dockerfile | 2 +- .../preview_document_page_controller.rb | 6 ----- lib/templates/process_document.rb | 27 ++++++++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index acf9dfbf..c66d5819 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,7 +31,7 @@ ENV BUNDLE_WITHOUT="development:test" WORKDIR /app -RUN apk add --no-cache build-base sqlite-dev libpq-dev mariadb-dev vips-dev vips-poppler vips-heif libc6-compat ttf-freefont ttf-liberation && cp /usr/share/fonts/liberation/LiberationSans-Regular.ttf /usr/share/fonts/liberation/LiberationSans-Bold.ttf / && apk del ttf-liberation +RUN apk add --no-cache build-base sqlite-dev libpq-dev mariadb-dev vips-dev vips-poppler poppler-utils vips-heif libc6-compat ttf-freefont ttf-liberation && cp /usr/share/fonts/liberation/LiberationSans-Regular.ttf /usr/share/fonts/liberation/LiberationSans-Bold.ttf / && apk del ttf-liberation COPY ./Gemfile ./Gemfile.lock ./ diff --git a/app/controllers/preview_document_page_controller.rb b/app/controllers/preview_document_page_controller.rb index f49a8128..8c51507e 100644 --- a/app/controllers/preview_document_page_controller.rb +++ b/app/controllers/preview_document_page_controller.rb @@ -6,12 +6,6 @@ class PreviewDocumentPageController < ActionController::API FORMAT = Templates::ProcessDocument::FORMAT def show - if Docuseal.multitenant? - Rollbar.warning('load page') - - return head :not_found - end - attachment = ActiveStorage::Attachment.find_by(uuid: params[:attachment_uuid]) return head :not_found unless attachment diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index 133e06b3..40f1aeac 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -9,12 +9,7 @@ module Templates PDF_CONTENT_TYPE = 'application/pdf' Q = 35 MAX_WIDTH = 1400 - MAX_NUMBER_OF_PAGES_PROCESSED = - if Docuseal.multitenant? - 70 - else - 40 - end + MAX_NUMBER_OF_PAGES_PROCESSED = 30 module_function @@ -74,10 +69,24 @@ module Templates end def generate_pdf_preview_from_file(attachment, file_path, page_number) - page = Vips::Image.new_from_file(file_path, dpi: DPI, page: page_number) - page = page.resize(MAX_WIDTH / page.width.to_f) + io = StringIO.new + + command = [ + 'pdftocairo', '-jpeg', '-jpegopt', "progressive=y,quality=#{Q},optimize=y", + '-scale-to-x', MAX_WIDTH, '-scale-to-y', '-1', + '-r', DPI, '-f', page_number + 1, '-l', page_number + 1, + '-singlefile', Shellwords.escape(file_path), '-' + ].join(' ') + + Open3.popen3(command) do |_, stdout, _, _| + io.write(stdout.read) + + io.rewind + end + + page = Vips::Image.new_from_buffer(io.read, '') - io = StringIO.new(page.write_to_buffer(FORMAT, Q: Q, interlace: true)) + io.rewind ApplicationRecord.no_touching do ActiveStorage::Attachment.create!(