David PHAM-VAN

Add support for Metadata XML

... ... @@ -7,6 +7,7 @@
- Improve GraphicState
- Add SVG Color filter
- Implement Compressed XREF
- Add support for Metadata XML
## 3.1.0
... ...
... ... @@ -30,6 +30,7 @@ export 'src/pdf/graphic_state.dart';
export 'src/pdf/graphics.dart';
export 'src/pdf/image.dart';
export 'src/pdf/info.dart';
export 'src/pdf/metadata.dart';
export 'src/pdf/outline.dart';
export 'src/pdf/page.dart';
export 'src/pdf/page_format.dart';
... ...
... ... @@ -17,6 +17,7 @@
import 'annotation.dart';
import 'data_types.dart';
import 'document.dart';
import 'metadata.dart';
import 'names.dart';
import 'object_dict.dart';
import 'outline.dart';
... ... @@ -38,6 +39,9 @@ class PdfCatalog extends PdfObjectDict {
/// The outlines of the document
PdfOutline? outlines;
/// The document metadata
PdfMetadata? metadata;
/// The initial page mode
final PdfPageMode pageMode;
... ... @@ -66,6 +70,10 @@ class PdfCatalog extends PdfObjectDict {
params['/Outlines'] = outlines!.ref();
}
if (metadata != null) {
params['/Metadata'] = metadata!.ref();
}
// the Names object
params['/Names'] = names.ref();
... ...
... ... @@ -595,15 +595,34 @@ class PdfDict<T extends PdfDataType> extends PdfDataType {
}
class PdfDictStream extends PdfDict<PdfDataType> {
const PdfDictStream({
factory PdfDictStream({
required PdfObject object,
Map<String, PdfDataType>? values,
Uint8List? data,
bool isBinary = false,
bool encrypt = true,
bool compress = true,
}) {
return PdfDictStream.values(
object: object,
values: values ?? {},
data: data ?? Uint8List(0),
encrypt: encrypt,
compress: compress,
isBinary: isBinary,
);
}
PdfDictStream.values({
required this.object,
Map<String, PdfDataType> values = const <String, PdfDataType>{},
required Map<String, PdfDataType> values,
required this.data,
this.isBinary = false,
this.encrypt = true,
this.compress = true,
}) : super.values(values);
final Uint8List data;
Uint8List data;
final PdfObject object;
... ... @@ -611,6 +630,8 @@ class PdfDictStream extends PdfDict<PdfDataType> {
final bool encrypt;
final bool compress;
@override
void output(PdfStream s) {
final _values = PdfDict(values);
... ... @@ -620,7 +641,7 @@ class PdfDictStream extends PdfDict<PdfDataType> {
if (_values.containsKey('/Filter')) {
// The data is already in the right format
_data = data;
} else if (object.pdfDocument.deflate != null) {
} else if (compress && object.pdfDocument.deflate != null) {
// Compress the data
final newData = Uint8List.fromList(object.pdfDocument.deflate!(data));
if (newData.lengthInBytes < data.lengthInBytes) {
... ...
... ... @@ -123,6 +123,7 @@ class PdfDocument {
/// This is the info object. Although this is an optional object, we
/// include it.
@Deprecated('This can safely be removed.')
PdfInfo? info;
/// This is the Pages object, which is required by each Pdf Document
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:convert';
import 'dart:typed_data';
import 'package:xml/xml.dart';
import 'data_types.dart';
import 'document.dart';
import 'object.dart';
/// Pdf Metadata
class PdfMetadata extends PdfObject<PdfDictStream> {
/// Store an Xml object
PdfMetadata(
PdfDocument pdfDocument,
this.metadata,
) : super(
pdfDocument,
params: PdfDictStream(
object: pdfDocument.catalog,
compress: false,
encrypt: false,
),
) {
pdfDocument.catalog.metadata = this;
}
final XmlDocument metadata;
@override
void prepare() {
super.prepare();
params['/SubType'] = const PdfName('/XML');
params.data = Uint8List.fromList(utf8.encode(metadata.toString()));
}
}
... ...
... ... @@ -36,7 +36,7 @@ class PdfObjectStream extends PdfObjectDict {
@override
void writeContent(PdfStream os) {
PdfDictStream(
PdfDictStream.values(
object: this,
isBinary: isBinary,
values: params.values,
... ...
... ... @@ -52,10 +52,10 @@ class PdfOutput {
final xref = PdfXrefTable();
/// This is used to track the /Root object (catalog)
PdfObject? rootID;
PdfCatalog? rootID;
/// This is used to track the /Info object (info)
PdfObject? infoID;
PdfInfo? infoID;
/// This is used to track the /Encrypt object (encryption)
PdfEncryption? encryptID;
... ...
... ... @@ -17,6 +17,7 @@
import 'dart:typed_data';
import 'package:pdf/pdf.dart';
import 'package:xml/xml.dart';
import 'page.dart';
import 'theme.dart';
... ... @@ -34,6 +35,7 @@ class Document {
String? subject,
String? keywords,
String? producer,
XmlDocument? metadata,
}) : document = PdfDocument(
pageMode: pageMode,
deflate: deflate,
... ... @@ -46,7 +48,7 @@ class Document {
subject != null ||
keywords != null ||
producer != null) {
document.info = PdfInfo(
PdfInfo(
document,
title: title,
author: author,
... ... @@ -56,6 +58,9 @@ class Document {
producer: producer,
);
}
if (metadata != null) {
PdfMetadata(document, metadata);
}
}
Document.load(
... ... @@ -82,7 +87,7 @@ class Document {
subject != null ||
keywords != null ||
producer != null) {
document.info = PdfInfo(
PdfInfo(
document,
title: title,
author: author,
... ...
... ... @@ -28,7 +28,7 @@ void main() {
img.fillRange(0, img.length - 1, 0x12345678);
final pdf = PdfDocument();
pdf.info = PdfInfo(pdf,
PdfInfo(pdf,
author: 'David PHAM-VAN',
creator: 'David PHAM-VAN',
title: 'My Title',
... ...