def get_image_from_pdf(document, node=None, preset_id=None): ext = file_formats.PNG with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_image_from_pdf_page(document.file_on_disk.name, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id)
def get_image_from_pdf(document, node=None, preset_id=None): ext = file_formats.PNG with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_image_from_pdf_page(document.file_on_disk.name, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id)
def get_image_from_pdf(document, node=None, preset_id=None): ext = file_formats.PNG orig_ext = document.file_format.extension with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf, tempfile.NamedTemporaryFile(suffix=".{}".format(orig_ext)) as localtempf: # localtempf is where we store the file in case it's in object storage shutil.copyfileobj(document.file_on_disk, localtempf) tempf.close() localtempf.flush() create_image_from_pdf_page(localtempf.name, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id, uploaded_by=document.uploaded_by)
def get_image_from_pdf(document, node=None, preset_id=None): ext = file_formats.PNG orig_ext = document.file_format.extension with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf, tempfile.NamedTemporaryFile(suffix=".{}".format(orig_ext)) as localtempf: # localtempf is where we store the file in case it's in object storage shutil.copyfileobj(document.file_on_disk, localtempf) tempf.close() localtempf.flush() create_image_from_pdf_page(localtempf.name, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id, uploaded_by=document.uploaded_by)
def extractor_fun(self, fpath_in, thumbpath_out, **kwargs): create_image_from_pdf_page(fpath_in, thumbpath_out, **kwargs)
def test_generates_16_9_thumbnail(self, tmpdir): input_file = os.path.join(files_dir, "generate_thumbnail", "sample.pdf") assert os.path.exists(input_file) output_file = tmpdir.join("pdf_16_9.png").strpath images.create_image_from_pdf_page(input_file, output_file, crop='smart') self.check_16_9_format(output_file)
def test_generates_thumbnail(self, tmpdir): input_file = os.path.join(files_dir, "generate_thumbnail", "sample.pdf") assert os.path.exists(input_file) output_file = tmpdir.join("pdf.png").strpath images.create_image_from_pdf_page(input_file, output_file) self.check_thumbnail_generated(output_file)
def test_raises_for_invalid_pdf(self, tmpdir, bad_pdf_file): input_file = bad_pdf_file.name output_file = tmpdir.join('thumbnail.png').strpath with pytest.raises(images.ThumbnailGenerationError): images.create_image_from_pdf_page(input_file, output_file)
def test_raises_for_missing_file(self, tmpdir): input_file = os.path.join(files_dir, 'file_that_does_not_exist.pdf') assert not os.path.exists(input_file) output_file = tmpdir.join('thumbnail.png').strpath with pytest.raises(images.ThumbnailGenerationError): images.create_image_from_pdf_page(input_file, output_file)