Create PDF viewer for a couple of hours
For a long time did not write about Qt, so do something simple but powerful. The framework has been established for over ten years (soon 20), but still continues to please us and to surprise thanks to Qt community efforts.
I want to show an example of application development with the expenditure of little effort at the junction of creating desktop applications technologies and web programming.
a few weeks ago I was looking for a way to convert PDF documents to specific images with the possibility of automation and scripting in the future. Of course there is an old-timer – package ImageMagic with utility convert, but unfortunately I was faced with the fact that this tool is not as good as I expected it to these files – do not render correctly, many files and that is not happy – Many illustrations have been spoiled p. >
I began to look for other tools and even all kinds of tools a lot but each has its own characteristics so that I did not choose which one to use.
Instead, I had an idea, if Qt is how pretty mature technology help me? In Qt it is very easy to create a PDF document using QPrinter, but what about the reverse functionality – make an image from a PDF page? But there is another well-conceived technology – PDF.js
Is it possible to combine these two technologies.? Sure! Qt has QWebEngineView component. We demonstrate in the code,
Quick based QMainWindow,
The result is a window with a web page inside. Now turn PDF.js. The project has a rather large amount of code, but it is possible to assemble a compact (minified) version which can be easily integrated into the website. assembly example,
The result is the compiled version pdf.js to build / minified folder, which is copied to our project. Set starting URL to a local file minified / web / viewer.html
To add a viewer.html inclusion qwebchannel.js. To add viewer.js initialization QWebChannel and get the channel name of the file that will be used instead of the default file,
Copy the minified version of the project folder. Finish getting a list of files from the command line arguments, and so on.