Skip to content

Commit

Permalink
[native_pdf_renderer] Fixed bug with render same page from issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeShkurko committed Oct 31, 2019
1 parent 6270613 commit c87e490
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/native_pdf_renderer/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.idea/
.dart_tool/
*/ios/Flutter/flutter_export_environment.sh

.packages
.pub/
Expand Down
3 changes: 1 addition & 2 deletions packages/native_pdf_renderer/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"java.configuration.updateBuildConfiguration": "interactive"
}
}
5 changes: 5 additions & 0 deletions packages/native_pdf_renderer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.5.0

* Added crop option for rendering (#11)
* Fixed bug with render same page from issue #5

## 1.4.2

* Fixed not correctly filling background Color on IOS
Expand Down
59 changes: 34 additions & 25 deletions packages/native_pdf_renderer/lib/src/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'dart:async';
import 'dart:typed_data' show Uint8List;

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'page.dart';

class PDFDocument {
PDFDocument._({
this.sourceName,
this.id,
this.pagesCount,
}) : _pages = List<PDFPage>(pagesCount);
const PDFDocument._({
@required this.sourceName,
@required this.id,
@required this.pagesCount,
});

static const MethodChannel _channel = MethodChannel('io.scer.pdf.renderer');

Expand All @@ -25,8 +26,6 @@ class PDFDocument {
/// Starts from 1.
final int pagesCount;

final List<PDFPage> _pages;

Future<void> close() => _channel.invokeMethod('close.document', id);

static PDFDocument _open(Map<dynamic, dynamic> obj, String sourceName) =>
Expand All @@ -37,33 +36,43 @@ class PDFDocument {
);

static Future<PDFDocument> openFile(String filePath) async => _open(
await _channel.invokeMethod('open.document.file', filePath),
await _channel.invokeMethod<Map<dynamic, dynamic>>(
'open.document.file',
filePath,
),
'file:$filePath');

static Future<PDFDocument> openAsset(String name) async => _open(
await _channel.invokeMethod('open.document.asset', name), 'asset:$name');
await _channel.invokeMethod<Map<dynamic, dynamic>>(
'open.document.asset',
name,
),
'asset:$name',
);

static Future<PDFDocument> openData(Uint8List data) async => _open(
await _channel.invokeMethod('open.document.data', data), 'memory:$data');
await _channel.invokeMethod<Map<dynamic, dynamic>>(
'open.document.data',
data,
),
'memory:$data',
);

/// Get page object. The first page is 1.
Future<PDFPage> getPage(int pageNumber) async {
if (pageNumber < 1 || pageNumber > pagesCount) return null;
var page = _pages[pageNumber - 1];
if (page == null) {
final obj = await _channel
.invokeMethod('open.page', {'documentId': id, 'page': pageNumber});
if (obj is Map<dynamic, dynamic>) {
page = _pages[pageNumber - 1] = PDFPage(
document: this,
id: obj['id'] as String,
pageNumber: pageNumber,
width: obj['width'] as int,
height: obj['height'] as int,
);
}
}
return page;
final obj =
await _channel.invokeMethod<Map<dynamic, dynamic>>('open.page', {
'documentId': id,
'page': pageNumber,
});
return PDFPage(
document: this,
id: obj['id'] as String,
pageNumber: pageNumber,
width: obj['width'] as int,
height: obj['height'] as int,
);
}

@override
Expand Down
37 changes: 22 additions & 15 deletions packages/native_pdf_renderer/lib/src/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:meta/meta.dart';
import 'document.dart';
import 'page_image.dart';

class PDFPageFormat<int> extends Enum<int> {
class PDFPageFormat extends Enum<int> {
const PDFPageFormat(int val) : super(val);

static const PDFPageFormat JPEG = PDFPageFormat(0);
Expand All @@ -16,21 +16,25 @@ class PDFPageFormat<int> extends Enum<int> {
}

class PDFCropDef {
final int x;
final int y;
final int width;
final int height;

PDFCropDef({@required this.x, @required this.y, @required this.width, @required this.height});
const PDFCropDef({
@required this.x,
@required this.y,
@required this.width,
@required this.height,
});

final int x, y;
final int width, height;
}

class PDFPage {
PDFPage(
{@required this.document,
@required this.id,
@required this.pageNumber,
@required this.width,
@required this.height});
const PDFPage({
@required this.document,
@required this.id,
@required this.pageNumber,
@required this.width,
@required this.height,
});

static const MethodChannel _channel = MethodChannel('io.scer.pdf.renderer');
final PDFDocument document;
Expand Down Expand Up @@ -85,6 +89,9 @@ class PDFPage {
int get hashCode => document.hashCode ^ pageNumber;

@override
String toString() =>
'$runtimeType{document: $document, page: $pageNumber, width: $width, height: $height}';
String toString() => '$runtimeType{'
'document: $document, '
'page: $pageNumber, '
'width: $width, '
'height: $height}';
}
10 changes: 7 additions & 3 deletions packages/native_pdf_renderer/lib/src/page_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:meta/meta.dart';
import 'page.dart';

class PDFPageImage {
PDFPageImage._({
const PDFPageImage._({
@required this.id,
@required this.pageNumber,
@required this.width,
Expand Down Expand Up @@ -86,6 +86,10 @@ class PDFPageImage {
int get hashCode => identityHashCode(id) ^ pageNumber;

@override
String toString() =>
'$runtimeType{id: $id, page: $pageNumber, width: $width, height: $height, bytesLength: ${bytes.lengthInBytes}}';
String toString() => '$runtimeType{'
'id: $id, '
'page: $pageNumber, '
'width: $width, '
'height: $height, '
'bytesLength: ${bytes.lengthInBytes}}';
}
6 changes: 3 additions & 3 deletions packages/native_pdf_renderer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: native_pdf_renderer
description: Flutter plugin to render PDF pages as images on both Android and iOS devices.
version: 1.4.2
version: 1.5.0
author: Serge Shkurko <[email protected]>
homepage: https://github.com/rbcprolabs/flutter_plugins/tree/master/packages/native_pdf_renderer

Expand All @@ -10,8 +10,8 @@ environment:
dependencies:
flutter:
sdk: flutter
meta: ^1.1.6
extension: ^0.0.4
meta: ^1.1.7
extension: ^0.0.5

flutter:
plugin:
Expand Down

0 comments on commit c87e490

Please sign in to comment.