Create a PDF viewer for a couple of hours

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

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

It works perfectly, the right approach, but it shows the default PDF file. How can I transfer the file name in javascript environment? To do this, Qt has another great QWebChannel module. The idea is that on the side of C ++ / Qt created QWebChannel object and he set the channel (channel) for the loaded web page. With this channel we can register the objects that will be available inside of JavaScript code. Of JavaScript will be visible Q_PROPERTY properties,

The above code allows access to the communicator object from JavaScript. Now you need to make changes to viewer.html / viewer.js, add a standard qwebchannel.js to make communication – is quite simple,

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,

Here’s how it works, The attached web channel and recorded communicator object before the page is loading. Then, when viewer.html loaded for the first time, determined QWebChannel JS class. After determining DEFAULT_URL created JS QWebChannel object and once communication is established, is called Sticky js function that reads the URL from the communicator object. The new URL will be used instead of the example file. In the same way you can pass otrenderinuyu page as an image of JavaScript in C ++ / Qt part of the application.

Copy the minified version of the project folder. Finish getting a list of files from the command line arguments, and so on.