Coder Social home page Coder Social logo

develtar / qt-pdf-viewer-library Goto Github PK

View Code? Open in Web Editor NEW
39.0 2.0 9.0 4.04 MB

The qt-pdf-viewer-library is a qml wrapper of pdf.js library which allows you to render PDF files in a WebView. This library also works on Android devices, and it uses WebSocketServer, WebSocketTransport and WebChannel to exchange data between qml and the browser environment.

License: Apache License 2.0

Java 21.35% CMake 0.42% C++ 11.47% QMake 0.89% AIDL 0.09% QML 1.88% JavaScript 59.79% CSS 3.20% HTML 0.89%
android open-source opensource pdf pdf-viewer pdfjs qt qt5

qt-pdf-viewer-library's Introduction

Qt Pdf Viewer Library

license latest-release stars

device-android device-desktop code-size

The qt-pdf-viewer-library is a qml wrapper of pdf.js library which allows you to render PDF files in a WebView. This library also works for Android devices, and it uses WebSocketServer, WebSocketTransport and WebChannel to exchange data between qml and the browser environment.

1.0. Dependencies

The qt modules needed by the library are:

  • webchannel
  • websockets
  • webview

1.0.1. Libraries

The libraries used to create the qt-pdf-viewer-library are:

1.1. Testing

The qt-pdf-viewer-library has been tested on the following qt versions:

  • Qt 5.12.4 (Desktop)
  • Qt 5.15.2 (Desktop and Android)
  • Qt 6.2.2 (Desktop)

on the following Android devices:

  • Galaxy Nexus, API 22, Android 5.1
  • Nexus S, API 23, Android 6.0
  • Nexus S, API 24, Android 7.0
  • Pixel 2 XL, API 27, Android 8.1
  • Pixel 4, API 28, Android 9.0
  • Redmi Note 8 pro, API 29, Android 10
  • Nexus 5X, API 29, Android 10
  • Nexus 10, API 30, Android 11
  • Pixel XL, API 30, Android 11

and on the following os:

  • Linux KDE 20.04

1.2. Usage

To use the qt-pdf-viewer-library in your app, follow these steps:

  • include the library in your .pro:
QML_IMPORT_PATH += $$PWD/libs/qt-pdf-viewer-library/
QML_DESIGNER_IMPORT_PATH += $$PWD/libs/qt-pdf-viewer-library/
include($$PWD/libs/qt-pdf-viewer-library/qtpdfviewer.pri)
  • initialize the library in your main.cpp just before QGuiApplication instantiation, and connect the aboutToQuit signal, emitted on app close, with the QtPdfViewerInitializer singleton deleteInstance method to allow the correct deletion of the initializer instance:
int main(int argc, char *argv[])
{
  ...

  // Initialize QtPdfViewer library
  // To make the pdf module to function correctly across all platforms,
  // it is necessary to call QtPdfViewerInitializer::initialize()
  // before in Qt>= 5.15.0, or after in Qt<5.15.0, creating
  // the QGuiApplication instance
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
  LTDev::QtPdfViewerInitializer::initialize();
  QGuiApplication app(argc, argv);
#else
  QGuiApplication app(argc, argv);
  LTDev::QtPdfViewerInitializer::initialize();
#endif

  // Delete QtPdfViewer instance on app close
  QObject::connect(&app, &QGuiApplication::aboutToQuit, LTDev::QtPdfViewerInitializer::getInstance(), LTDev::QtPdfViewerInitializer::deleteInstance);

  ...
}
  • then, in qml import the library import it.ltdev.qt.qml.components 1.0, and use the provided PdfView class.
import it.ltdev.qt.qml.components 1.0 as LTDev

ApplicationWindow {
      ...

      LTDev.PdfView {
          id: pdfView

          anchors.fill: parent

          onViewerLoaded: {
              // Load pdf only when viewer is ready
              pdfView.load("path/to/my/document.pdf")
          }

          onPdfLoaded: {
              // Pdf has been correctly loaded
          }

          onError: {
             // Some error occurred
             console.error("Error: ", message)
          }
      }
}
  • (Android only) since Android 9 or higher, in order to view remote document from an HTTP url, is necessary to add android:usesCleartextTraffic="true" in the application tag of the manifest.xml. Since that version cleartext support is disabled by default.
<application android:usesCleartextTraffic="true" ...>
  • (Android only) on Android permission android.permission.READ_EXTERNAL_STORAGE must be added in the manifest.xml, and since Android 9, it must be also asked at runtime (see sample app).
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

1.3. PdfView

The provided PdfView exposes the following properties:

  • page: the current page number

  • pages: the total document pages

  • thumbnails: the list of pages as base64 images

  • scale: the current scale value

  • scrollModes: the available scroll modes (0, 1, 2, which corresponds respectively to vertical, horizontal and wrapped modes)

  • scrollMode: the current scroll mode

  • scalesMode: the available scale modes ("page-actual", "auto", "page-fit", "page-width", "page-height")

  • scaleMode: the current scale mode

  • toolModes: the availbale tool modes (0, 1, which corresponds respectively to cursor and hand modes)

  • toolMode: the current tool mode

the following methods:

  • reloadViewer(): reloads the viewer

  • load(path): loads the given pdf document. The document is converted in a base64 string to be loaded in the view.

  • setPage(page): sets the page of the document

  • previousPage(): sets the page of the document to the previous one

  • nextPage(): sets the page of the document to the next one

  • setScrollMode(scrollMode): sets the scroll mode of the document

  • setScaleMode(scaleMode): sets the scale mode of the document

  • setToolMode(toolMode): sets the tool mode of the document

  • rotate(angle): rotates the document of the current angle

  • zoomIn(): zooms the document in

  • zoomOut(): zooms the document out

  • searchText(query, phraseSearch, caseSensitive, highlightAll, findPrevious): searches for the given text in the pdf document.

    • query: the text to search for

    • phraseSearch: true if entire phrase must be searched, false otherwise

    • caseSensitive: true if case sensitive must be enabled, false otherwise

    • highlightAll: true if matches found must be highlighted, false otherwise

    • findPrevious: true if previous matches must be considered, false otherwise

  • searchTextOccurance(query, phraseSearch, caseSensitive, highlightAll, findPrevious): searches for the next occurance of the given text in the pdf document

and the following signals:

  • error(var message): emitted when some error occurs during pdf loading. The message parameter is a json string which contains the error's informations.

  • pdfLoaded(): emitted when pdf is entirely loaded

  • viewerLoaded(): emitted when viewer is loaded and ready

1.4. Sample App

The sample app implements all operations provided by the library, like rotation, fit size dimensions, zoom in/out, change view visualization, text search and pages preview.

Below some screenshot for the desktop version:

qt-pdf-viewer-library's People

Contributors

actions-user avatar develtar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qt-pdf-viewer-library's Issues

About qt-pdf-viewer-library

Hello,

I tried your application using Qt 5.15.2/MSVC2019. UI launches, but sample.pdf nevers shows up.... Only getting the main toolbar and a large gray area below. Doesn't seem to work as expected. Qt standard error is not showing error...

Questions:

  • How do I make it work?
  • How to load another file than sample.pdf?

Thanks for your help.

Best regards.

A question about opening a new file.

Hello, in one of my project (https://github.com/ic005k/Knot) integrates the library, but found a problem, is to open the second or more files, there will be a blank, during normal open it again.
At the moment, I get around this problem by opening an empty file before opening one, but I don't think this is a scientific solution.
What do you think about that? thank you.

pdfView.load("") pdfView.load(pdfPath)

https://github.com/ic005k/Knot/blob/e19e5ceed53f8bc9e3a3d1f19588e1cb159640ea/pdf_module/PdfPage.qml#L35

Step-By-Step

How to set it up step-by-step. I am new to qt so stuck at various portions. Help me please.
qt_ss

There are two details.

Hello, I have two details as follows:

  1. The APP can automatically save the location of the PDF document opened last time on the PC side, but on the Android side, it seems that it can't save the status of the document. May I ask what I missed?
  2. It seems that two-finger scaling (zoom in or out with fingers) cannot be realized on Android. How can I achieve this function?
    Thanks again.

module "it.ltdev.qt.qml.components" is not installed

First of all thank you for doing such a great job.

When I load "PdfPage.qml" into a quickWidget, it always reports the following error:

qrc:/sample/PdfPage.qml:10:1: module "it.ltdev.qt.qml.components" is not installed
The import it.ltdev.qt.qml.com 1.0 as LTDev ponents

I tried to install the module using the following method:
engine-> AddImportPath: ("/");
or
Qputenv: (" QML2_IMPORT_PATH ", "/");
It still doesn't work.

However, when I used QQmlApplicationEngine to load "PdfPage.qml", everything worked perfectly.

Do you have any suggestions, please? Thank you very much!

Also, can simplify the use of this module ponents "it.ltdev.qt.qml.com"? I found it to have a lot of hierarchical directories, which was a little confusing.

Thanks again.

Web page not available

I compiled the example available with your project. The application compiles and runs on the Android device. The problem is that the web view reports that the web page cannot be loaded: file:///data/user/0/it.ltdev.qt.pdfviewer/files/libs/pdfjs/viewer.html.
At the web page of Qt Creator, the application reports : I chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: loadDocument is not defined", source: chrome-error://chromewebdata/ (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.