Package: preview-generator
Presentationpreview-generator is a library for generating preview - thumbnails, pdf, text and json overview
for all your file-based content. This module gives you access to jpeg, pdf, text, html and json
preview of virtually any kind of file. It also includes a cache mechanism so you do not have to
care about preview storage.By creating this module, the goal was to delegate the responsibility of building preview
of files managed bytracim.The changelog is availablein a separate file.Supported file formatsSupported mimetypes depends on builders and dependencies installed.full supported list file format here:supported mimetypes list.Preview-generator has builders for:ImagesRaster graphic image formats : png, jpeg, bmp, tiff, gif, …Vectorial graphic format: svgCamera raw format: dng, arw, …Office/Text Documentsimple text file : txt, json, …office document: odt, doc, docxpdf documentThose file formats are generated using libreoffice.
The preview generation has a default timeout of 60 seconds.
It is possible to change this timeout by setting theLIBREOFFICE_PROCESS_TIMEOUTenvironment variable to a number of seconds.
Setting a zero or negative value for this variable will disable the timeout.Archive filezip, tar, …3D Filesupport for geometric file: .ply, .obj, .stl, .gltf/.glbVideo formatsupport for multiples image preview of video : .mp4, .flv, .webm, .ogv, .wmv, etc.InstallationMandatory Dependencies:On debian :apt-get install poppler-utils libfile-mimeinfo-perl libimage-exiftool-perl ghostscript libsecret-1-0 zlib1g-dev libjpeg-dev imagemagick libmagic1 webpinstall preview_generator without external addons:pip install preview-generatorTo install all previews builders dependencies:pip install preview-generator[all]
sudo apt-get install libreoffice inkscape ffmpeg xvfb
DRAWIO_VERSION="15.7.3" && curl -LO https://github.com/jgraph/drawio-desktop/releases/download/v${DRAWIO_VERSION}/drawio-x86_64-${DRAWIO_VERSION}.AppImage && mv drawio-x86_64-${DRAWIO_VERSION}.AppImage /usr/local/bin/drawioTo check dependencies, you can run:preview --check-dependenciesOffice Files (LibreOffice)apt-get install libreofficeDTP(Scribus)If you need to preview scribus.slafiles you will need scribus >= 1.5.On debian :apt-get install scribus xvfb
pip install preview-generator[scribus]If scribus >=1.5 is not available in your distribution you can use an AppImage.Download the last AppImage from the official websitehttps://www.scribus.net/downloads/unstable-branch/mv /path/to/image/scribus-x.y.appimage /usr/local/bin/scribus
chmod +x /usr/local/bin/scribusVector Images (Inkscape)on debian:apt-get install inkscapeVector Images (cairosvg)pip install preview-generator[cairosvg]Video(ffmpeg)On debian :apt-get install ffmpeg
pip install preview-generator[video]RAW Images(ufraw-batch)On debian :pip install preview-generator[raw]Diagram(DrawIO)install xvfbinstalldraw-io package:install python specific dependencies:on debian:apt install xvfb
DRAWIO_VERSION="15.7.3" && curl -LO https://github.com/jgraph/drawio-desktop/releases/download/v${DRAWIO_VERSION}/drawio-x86_64-${DRAWIO_VERSION}.AppImage && mv drawio-x86_64-${DRAWIO_VERSION}.AppImage /usr/local/bin/drawio
pip install preview-generator[drawio]3D files (VTK)warning:VTK lib provided from pypi may not be builded for latest python version. You can either decide to downgrade python version or build VTK yourself to make things work as expected.On debian :pip install preview-generator[3D]warning:GLTF support is disabled by default due to segfault issues with non-embedded gltf file, you must setGLTF_EXPERIMENTAL_SUPPORT_ENABLEDenv var to1, to enable it.HEIC supportBuilding ImageMagick with heic support:Building ImageMagick with heic support.EPS supportYou need to edit the policies of ImageMagick in /etc/ImageMagick-*/policy.xml.<policy domain="coder" rights="none" pattern="ESP" />Just wrap it between <!– and –> to comment it.UsageHere are some examples of codeBasic UsageMost basic usage, create a jpeg from a png, default size 256x256frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'file_to_preview_path='/tmp/an_image.png'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(file_to_preview_path)Preview an image with a specific sizeYou can choose the size of your image using params width and height.frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'file_to_preview_path='/tmp/an_image.png'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(file_to_preview_path,width=1000,height=500)Preview a pdf or an office document as a jpegfrompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'pdf_or_odt_to_preview_path='/tmp/a_pdf.pdf'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(pdf_or_odt_to_preview_path)By default it will generate the preview of the first page of the document.
Using paramspage, you can you pick the page you want to preview.page number starts at 0, if you want to preview the second page of your document then the argument will be 1 `page=1`frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'pdf_or_odt_to_preview_path='/tmp/a_pdf.pdf'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(pdf_or_odt_to_preview_path,page=1)Generate a pdf preview of a libreoffice text documentfrompreview_generator.managerimportPreviewManagermanager=PreviewManager('/tmp/cache/',create_folder=True)pdf_file_path=manager.get_pdf_preview('/home/user/Documents/report.odt',page=2)print('Preview created at path : ',thumbnail_file_path)For Office types into PDF :cache_path='/tmp/previews'preview_manager=PreviewManager(cache_path,create_folder=True)path_to_preview=preview_manager.get_pdf_preview(file_path,page=page_id)-> Will create a preview from an office file into a pdf fileargs :file_path : the String of the path where is the file you want to get the previewpage : the int of the page you want to get. If not mentioned all the pages will be returned. First page is page 0returns :str: path to the preview fileFor images(GIF, BMP, PNG, JPEG, PDF) into jpeg :cache_path='/tmp/previews'preview_manager=PreviewManager(cache_path,create_folder=True)path_to_preview=preview_manager.get_jpeg_preview(file_path,height=1024,width=526)-> Will create a preview from an image file into a jpeg file of size 1024 * 526args :file_path : the String of the path where is the file you want to get the previewheight : height of the preview in pixelswidth : width of the preview in pixels. If not mentioned, width will be the same as heightreturns :str: path to the preview fileOther conversions :The principle is the same as aboveZip to text or html :will build a list of files into text/html inside the jsonOffice to jpeg :will build the pdf out of the office file and then build the jpeg.Text to text :mainly just a copy stored in the cacheCommand LineFor test purposes, you can usepreviewfrom the command line,
giving the file to preview as a parameter:preview demo.pdfOr multiple files:preview *.pdfCache mechanismNaming :The name of the preview generated in the cache directory will be :{file_name}-[{size}-]{file_md5sum}[({page})]{extension}file_name = the name of the file you asked for a preview without the extension.size = the size you asked for the preview. In case of a Jpeg preview.file_md5sum = the md5sum of the entire path of the file. To avoid conflicts like files that have the same name but are in different directory.page = the page asked in case of pdf or office document preview.extensions = the extension of the preview (.jpeg for a jpeg, .txt for a text, etc)Example :These scripts :GIF to JPEG :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_preview=manager.get_jpeg_preview(file_path=current_dir+'the_gif.gif',height=512,width=512,)print('Preview created at path : ',path_to_preview)will printPreview created at path : the_gif-512x512-60dc9ef46936cc4fff2fe60bb07d4260.jpegODT to JPEG :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_file=manager.get_jpeg_preview(file_path=current_dir+'the_odt.odt',page=1,height=1024,width=1024,)print('Preview created at path : ',path_to_preview)will printPreview created at path : the_odt-1024x1024-c8b37debbc45fa96466e5e1382f6bd2e-page1.jpegZIP to Text :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_file=manager.get_text_preview(file_path=current_dir+'the_zip.zip',)print('Preview created at path : ',path_to_file)will printPreview created at path : the_zip-a733739af8006558720be26c4dc5569a.txtKnown IssuesInputExtensionNotFound or UnsupportedMimeTypeThe current mimetype/file-extension database of preview-generator may differ between systems which
means you can struggle to get previews depending on your OS or you installed software.In case you get one of these exceptions and the mimetype/extension you tried is marked as
supported by preview generator,  you should:
- check the version of PG you are using.
- check you have the proper dependencies to make you’re builder work as expected.
- check if the mimetype of the format you are using is handled by preview_generator mimetype_storage (python console):frompreview_generator.extensionimportmimetypes_storagemimetypes_storage.guess_all_extensions('application/vnd.openxmlformats-officedocument.wordprocessingml.document')['.docx']mimetypes_storage.guess_type('toto.docx',strict=False)('application/vnd.openxmlformats-officedocument.wordprocessingml.document',None)In case you don’t get proper result for your file_extension/mimetype, you can work around the issue this way:frompreview_generator.extensionimportmimetypes_storagemimetypes_storage.add_type('application/vnd.openxmlformats-officedocument.wordprocessingml.document','.docx')Feel free to propose an upstream patch to add the proper MimetypeMapping to the builder you’re using.Support for 3D file on headless serverStandard vtk library require an X server to run properly.
To make 3D file preview work on a headless server, you have two option:Untested: Compile/find a vtk version correctly compiled with flags to disable x requirement (with OSMesa support)Use an x framebuffer like xvfbFor the last one, this configuration is known to work:install packagexvfb.launchXvfb :99-screen0 1x1x16 > /dev/null2>&1&(note: this use a very small x framebuffer 1x1 pixel in 16 color depth to limit video usage).run python code with env varDISPLAY=:99.0Process Stuck during generation of big spreadsheet previewWith libreoffice version like 6.0.7.3, the generation of some big spreadsheet files (like xlsx) takes a very long time, until it reaches a 1 minute timeout.
The solution is to update libreoffice.During our internal tests, we found out that the spreadsheet file that failed to be generated in 6.0.7.3 is properly generated in 6.4.2.2. We advise you to
use at least this version to avoid this kind of issues.Contribute and Developer’s Kitseehere.LicenseMIT licensed.https://opensource.org/licenses/MIT