Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: output to file stream #1816

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pdf/lib/src/pdf/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import 'dart:io';
import 'dart:math' as math;
import 'dart:typed_data';

Expand Down Expand Up @@ -251,10 +252,22 @@ class PdfDocument {
}
}

/// Generate the PDF document in a file
Future<void> saveFile(File file) async {
return pdfCompute(() async {
final os = PdfStreamFile(file);
if (prev != null) {
os.putBytes(prev!.bytes);
}
await _write(os);
os.close();
});
}

/// Generate the PDF document as a memory file
Future<Uint8List> save() async {
return pdfCompute(() async {
final os = PdfStream();
final os = PdfStreamBuffer();
if (prev != null) {
os.putBytes(prev!.bytes);
}
Expand Down
4 changes: 2 additions & 2 deletions pdf/lib/src/pdf/format/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ abstract class PdfDataType {

void output(PdfObjectBase o, PdfStream s, [int? indent]);

PdfStream _toStream([int? indent]) {
final s = PdfStream();
PdfStreamBuffer _toStream([int? indent]) {
final s = PdfStreamBuffer();
output(
PdfObjectBase(
objser: 0,
Expand Down
2 changes: 1 addition & 1 deletion pdf/lib/src/pdf/format/diagnostic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mixin PdfDiagnostic {
void writeDebug(PdfStream os) {
assert(() {
if (_offset != null) {
final o = PdfStream();
final o = PdfStreamBuffer();
_properties.forEach(o.putComment);
final b = o.output();
os.setBytes(
Expand Down
102 changes: 80 additions & 22 deletions pdf/lib/src/pdf/format/stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,46 @@
* limitations under the License.
*/

import 'dart:io';
import 'dart:typed_data';

class PdfStream {
abstract class PdfStream {
void putByte(int s);

void putBytes(List<int> s);

void setBytes(int offset, Iterable<int> iterable);

void putStream(PdfStreamBuffer s);

int get offset;

void putString(String? s) {
assert(() {
for (final codeUnit in s!.codeUnits) {
if (codeUnit > 0x7f) {
return false;
}
}
return true;
}());
putBytes(s!.codeUnits);
}

void putComment(String s) {
if (s.isEmpty) {
putByte(0x0a);
} else {
for (final l in s.split('\n')) {
if (l.isNotEmpty) {
putBytes('% $l\n'.codeUnits);
}
}
}
}
}

class PdfStreamBuffer extends PdfStream {
static const int _grow = 65536;

Uint8List _stream = Uint8List(_grow);
Expand All @@ -34,50 +71,71 @@ class PdfStream {
_stream = newBuffer;
}

@override
void putByte(int s) {
_ensureCapacity(1);
_stream[_offset++] = s;
}

@override
void putBytes(List<int> s) {
_ensureCapacity(s.length);
_stream.setAll(_offset, s);
_offset += s.length;
}

@override
void setBytes(int offset, Iterable<int> iterable) {
_stream.setAll(offset, iterable);
}

void putStream(PdfStream s) {
@override
void putStream(PdfStreamBuffer s) {
putBytes(s._stream);
}

@override
int get offset => _offset;

Uint8List output() => _stream.sublist(0, _offset);
}

void putString(String? s) {
assert(() {
for (final codeUnit in s!.codeUnits) {
if (codeUnit > 0x7f) {
return false;
}
}
return true;
}());
putBytes(s!.codeUnits);
class PdfStreamFile extends PdfStream {
PdfStreamFile(File file) {
_raf = file.openSync(mode: FileMode.write);
}

void putComment(String s) {
if (s.isEmpty) {
putByte(0x0a);
} else {
for (final l in s.split('\n')) {
if (l.isNotEmpty) {
putBytes('% $l\n'.codeUnits);
}
}
}
late RandomAccessFile _raf;

void close() {
_raf.closeSync();
}

@override
void putByte(int s) {
_raf.writeByteSync(s);
}

@override
void putBytes(List<int> s) {
_raf.writeFromSync(s);
}

@override
void setBytes(int offset, Iterable<int> iterable) {
final originalOffset = _raf.positionSync();
_raf.setPositionSync(offset);
_raf.writeFromSync(iterable.toList());
_raf.setPositionSync(originalOffset);
}

@override
void putStream(PdfStreamBuffer s) {
putBytes(s._stream);
}

@override
int get offset {
return _raf.positionSync();
}
}
2 changes: 1 addition & 1 deletion pdf/lib/src/pdf/format/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PdfString extends PdfDataType {
}

factory PdfString.fromStream(
PdfStream value, {
PdfStreamBuffer value, {
PdfStringFormat format = PdfStringFormat.literal,
bool encrypted = true,
}) {
Expand Down
4 changes: 2 additions & 2 deletions pdf/lib/src/pdf/obj/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PdfChoiceField extends PdfAnnotWidget {
params['/V'] = const PdfNull();
}

final buf = PdfStream();
final buf = PdfStreamBuffer();
final g = PdfGraphics(page, buf);
g.setFillColor(textColor);
g.setFont(font, fontSize);
Expand Down Expand Up @@ -928,7 +928,7 @@ class PdfTextField extends PdfFormField {
params['/MaxLen'] = PdfNum(maxLength!);
}

final buf = PdfStream();
final buf = PdfStreamBuffer();
final g = PdfGraphics(page, buf);
g.setFillColor(textColor);
g.setFont(font, fontSize);
Expand Down
2 changes: 1 addition & 1 deletion pdf/lib/src/pdf/obj/object_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PdfObjectStream extends PdfObject<PdfDict> {
);

/// This holds the stream's content.
final PdfStream buf = PdfStream();
final PdfStreamBuffer buf = PdfStreamBuffer();

/// defines if the stream needs to be converted to ascii85
final bool isBinary;
Expand Down
11 changes: 11 additions & 0 deletions pdf/lib/src/widgets/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import 'dart:io';
import 'dart:typed_data';

import 'package:xml/xml.dart';
Expand Down Expand Up @@ -124,6 +125,16 @@ class Document {
_pages.add(page);
}

Future<void> saveFile(File file) async {
if (!_paint) {
for (final page in _pages) {
page.postProcess(this);
}
_paint = true;
}
await document.saveFile(file);
}

Future<Uint8List> save() async {
if (!_paint) {
for (final page in _pages) {
Expand Down
13 changes: 11 additions & 2 deletions pdf/lib/src/widgets/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Page {

PageOrientation get orientation => pageTheme.orientation;

final BuildCallback _build;
BuildCallback? _build;

ThemeData? get theme => pageTheme.theme;

Expand All @@ -81,6 +81,8 @@ class Page {

EdgeInsets? get resolvedMargin => margin?.resolve(pageTheme.textDirection);

bool processed = false;

@protected
void debugPaint(Context context) {
final _margin = resolvedMargin!;
Expand Down Expand Up @@ -113,6 +115,11 @@ class Page {
}

void postProcess(Document document) {
if (processed) {
return;
}
assert(_build != null);

final canvas = _pdfPage!.getGraphics();
canvas.reset();
final _margin = resolvedMargin;
Expand All @@ -139,7 +146,7 @@ class Page {
Widget? content;
Widget? foreground;

content = _build(context);
content = _build!(context);

final size = layout(content, context, constraints);

Expand Down Expand Up @@ -181,6 +188,8 @@ class Page {
if (foreground != null) {
paint(foreground, context);
}
processed = true;
_build = null;
}

@protected
Expand Down
Loading
Loading