Skip to content

Commit

Permalink
Flutter Web updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
espresso3389 committed Dec 5, 2023
1 parent 4791eb9 commit c50a215
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ migration:
- platform: macos
create_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
base_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
- platform: web
create_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
base_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
- platform: windows
create_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
base_revision: 9e1c857886f07d342cf106f2cd588bcd5e031bb2
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"Subrect",
"unawaited",
"unregister",
"vsync"
"vsync",
"xcframework"
],
"editor.codeActionsOnSave": {
"source.fixAll": "always",
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 0.0.1
## 0.1.0

* TODO: Describe initial release.
- First release (Documentation is not yet ready)
94 changes: 91 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
# pdfrx

pdfrx is a PDF viewer implementation that built on the top of pdfium.
The plugin currently supports Android, iOS, Windows, macOS, and Linux.
[pdfrx](https://pub.dartlang.org/packages/pdfrx) is yet another PDF viewer implementation that built on the top of [pdfium](https://pdfium.googlesource.com/pdfium/).
The plugin currently supports Android, iOS, Windows, macOS, Linux, and Web.

Please note that "Web" is not shown in [pub.dev](https://pub.dev/packages/pdfrx)'s platform list, but **IT ACTUALLY DOES SUPPORT** Web.

![](https://private-user-images.githubusercontent.com/1311400/288040209-c4c44fde-2fb7-4e45-9261-5e33c0d1a0a9.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTEiLCJleHAiOjE3MDE3ODAxNzIsIm5iZiI6MTcwMTc3OTg3MiwicGF0aCI6Ii8xMzExNDAwLzI4ODA0MDIwOS1jNGM0NGZkZS0yZmI3LTRlNDUtOTI2MS01ZTMzYzBkMWEwYTkuZ2lmP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQUlXTkpZQVg0Q1NWRUg1M0ElMkYyMDIzMTIwNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyMzEyMDVUMTIzNzUyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZTdlNmY1ODY5NWUwNjAzNzU3MWViZmU3ZDNkMGM4MTgxNWU4NmU3ZmU1NmRlNGZmYWZhNzZkNjQxNTQ5ZjdiZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.hU9zW_HQycBEC9N4heOQG7x9qc6IhSzJBIu3_4mZ7nA)

## What differences from [pdf_render](https://pub.dev/packages/pdf_render)?

- Desktop platforms support (Windows, macOS, Linux)
- Password protected PDF files support
- Multithreaded PDF rendering
- [pdfium](https://pdfium.googlesource.com/pdfium/) based structure that is designed to support more PDF features in incoming versions :)

## Getting Started

The following fragment illustrates the easiest way to show a PDF file in assets:

```dart
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Pdfrx example'),
),
body: PdfViewer(
document: PdfDocument.openAsset('assets/hello.pdf'),
),
),
);
}
}
```
flutter pub add pdfrx

## Install

Add this to your package's `pubspec.yaml` file and execute `flutter pub get`:

```yaml
dependencies:
pdfrx: ^0.1.0
```
### Web
For Web, you should add the following `<script>` block to your `index.html` just before `<script src="main.dart.js"... </script>` to load [PDF.js](https://mozilla.github.io/pdf.js/):

```html
<!-- IMPORTANT: load pdfjs files -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js"
type="text/javascript"
></script>
<script type="text/javascript">
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js";
pdfRenderOptions = {
// where cmaps are downloaded from
cMapUrl: "https://cdn.jsdelivr.net/npm/[email protected]/cmaps/",
// The cmaps are compressed in the case
cMapPacked: true,
// any other options for pdfjsLib.getDocument.
// params: {}
};
</script>
```

Please check [example's code](example/web/index.html) for the actual usage.

Please note that; with pdf.js 4.0 series, [they changed the way to load `pdfjsLib`](https://github.com/mozilla/pdf.js/issues/17228), it does not simply work with the current code. We need further investigation to support them.

## macOS

For macOS, Flutter app restrict its capability by enabling [App Sandbox](https://developer.apple.com/documentation/security/app_sandbox) by default. You can change the behavior by editing your app's entitlements files depending on your configuration. See [the discussion below](#deal-with-app-sandbox).

- [`macos/Runner/Release.entitlements`](https://github.com/espresso3389/flutter_pdf_render/blob/master/example/macos/Runner/Release.entitlements)
- [`macos/Runner/DebugProfile.entitlements`](https://github.com/espresso3389/flutter_pdf_render/blob/master/example/macos/Runner/DebugProfile.entitlements)

### Deal with App Sandbox

The easiest option to access files on your disk, set [`com.apple.security.app-sandbox`](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_app-sandbox) to `false` on your entitlements file though it is not recommended for releasing apps because it completely disables [App Sandbox](https://developer.apple.com/documentation/security/app_sandbox).

Another option is to use [`com.apple.security.files.user-selected.read-only`](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_files_user-selected_read-only) along with [file_selector_macos](https://pub.dev/packages/file_selector_macos). The option is better in security than the previous option.

Anyway, the example code for the plugin illustrates how to download and preview internet hosted PDF file. It uses
[`com.apple.security.network.client`](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_network_client) along with [flutter_cache_manager](https://pub.dev/packages/flutter_cache_manager):

```xml
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
```
14 changes: 0 additions & 14 deletions example/ios/Flutter/Generated.xcconfig

This file was deleted.

13 changes: 0 additions & 13 deletions example/ios/Flutter/flutter_export_environment.sh

This file was deleted.

19 changes: 0 additions & 19 deletions example/ios/Runner/GeneratedPluginRegistrant.h

This file was deleted.

28 changes: 0 additions & 28 deletions example/ios/Runner/GeneratedPluginRegistrant.m

This file was deleted.

Binary file added example/web/icons/Icon-maskable-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/web/icons/Icon-maskable-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
}
</script>
<!-- IMPORTANT: load pdfjs files -->
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@2.12.313/build/pdf.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.min.js" type="text/javascript"></script>
<script type="text/javascript">
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.12.313/build/pdf.worker.min.js";
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.worker.min.js";
pdfRenderOptions = {
// where cmaps are downloaded from
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.12.313/cmaps/',
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/cmaps/',
// The cmaps are compressed in the case
cMapPacked: true,
// any other options for pdfjsLib.getDocument.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/pdfrx_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ abstract class PdfDocument {
Future<PdfPage> getPage(int pageNumber);
}

/// Handles a PDF page in [PDFDocument].
/// Handles a PDF page in [PdfDocument].
abstract class PdfPage {
/// PDF document.
PdfDocument get document;
Expand All @@ -104,7 +104,7 @@ abstract class PdfPage {
/// Returned image should be disposed after use.
/// [x], [y], [width], [height] specify sub-area to render in pixels.
/// [fullWidth], [fullHeight] specify virtual full size of the page to render in pixels. If they're not specified, [width] and [height] are used to specify the full size.
/// If [width], [height], [fullWidth], [fullHeight], and [dpi] are all 0, the page is rendered at 72 dpi.
/// If [width], [height], [fullWidth], and [fullHeight], are all 0, the page is rendered at 72 dpi.
/// [backgroundColor] is used to fill the background of the page and if not specified, [Colors.white] is used.
/// ![](./images/render-params.png)
Future<PdfImage> render({
Expand Down

0 comments on commit c50a215

Please sign in to comment.