save method

Future<List<int>> save()

Saves the document and return the saved bytes as future list of int.

//Create a PDF document instance.
PdfDocument document = PdfDocument();
//Get the page and draw text.
document.pages.add().graphics.drawString(
    'Hello World!', PdfStandardFont(PdfFontFamily.helvetica, 12),
    brush: PdfBrushes.black, bounds: Rect.fromLTWH(0, 0, 0, 0));
//Save and dispose document.
List<int> bytes = await document.save();
document.dispose();

Implementation

Future<List<int>> save() async {
  final List<int> buffer = <int>[];
  final PdfWriter writer = PdfWriter(buffer);
  writer.document = this;
  await _checkPagesAsync();
  if (_helper.isLoadedDocument &&
      _bookmark != null &&
      _bookmark!.count > 0 &&
      _helper.crossTable.documentCatalog != null &&
      !_helper.crossTable.documentCatalog!
          .containsKey(PdfDictionaryProperties.outlines)) {
    _helper.catalog.setProperty(
        PdfDictionaryProperties.outlines, PdfReferenceHolder(_bookmark));
  } else if (_outlines != null &&
      _outlines!.count < 1 &&
      _helper.catalog.containsKey(PdfDictionaryProperties.outlines)) {
    _helper.catalog.remove(PdfDictionaryProperties.outlines);
  }
  if (PdfSecurityHelper.getHelper(security).encryptAttachments &&
      security.userPassword == '') {
    throw ArgumentError.value(
        'User password cannot be empty for encrypt only attachment.');
  }
  if (_helper.isLoadedDocument) {
    if (_helper._security != null) {
      if (PdfSecurityHelper.getHelper(_helper._security!)
          .encryptAttachments) {
        fileStructure.incrementalUpdate = false;
        if (PdfSecurityHelper.getHelper(_helper._security!)
            .encryptor
            .userPassword
            .isEmpty) {
          PdfSecurityHelper.getHelper(_helper._security!)
              .encryptor
              .encryptOnlyAttachment = false;
        }
      }
    }
    if (fileStructure.incrementalUpdate &&
        (_helper._security == null ||
            (_helper._security != null &&
                !PdfSecurityHelper.getHelper(_helper._security!)
                    .modifiedSecurity &&
                !PdfPermissionsHelper.isModifiedPermissions(
                    _helper._security!.permissions)))) {
      await _copyOldStreamAsync(writer).then((_) {
        if (_helper.catalog.changed!) {
          _readDocumentInfoAsync();
        }
      });
    } else {
      _helper.crossTable = PdfCrossTable.fromCatalog(
          _helper.crossTable.count,
          _helper.crossTable.encryptorDictionary,
          _helper.crossTable.pdfDocumentCatalog);
      _helper.crossTable.document = this;
      _helper.crossTable.trailer![PdfDictionaryProperties.info] =
          PdfReferenceHolder(documentInformation);
    }
    await _appendDocumentAsync(writer);
  } else {
    if (_helper.conformanceLevel == PdfConformanceLevel.a1b ||
        _helper.conformanceLevel == PdfConformanceLevel.a2b ||
        _helper.conformanceLevel == PdfConformanceLevel.a3b) {
      PdfDocumentInformationHelper.getHelper(documentInformation).xmpMetadata;
      if (_helper.conformanceLevel == PdfConformanceLevel.a3b &&
          _helper.catalog.names != null &&
          attachments.count > 0) {
        final PdfName fileRelationShip =
            PdfName(PdfDictionaryProperties.afRelationship);
        final PdfArray fileAttachmentAssociationArray = PdfArray();
        for (int i = 0; i < attachments.count; i++) {
          if (!PdfFileSpecificationBaseHelper.getHelper(attachments[i])
              .dictionary!
              .items!
              .containsKey(fileRelationShip)) {
            PdfFileSpecificationBaseHelper.getHelper(attachments[i])
                .dictionary!
                .items![fileRelationShip] = PdfName('Alternative');
          }
          fileAttachmentAssociationArray.add(PdfReferenceHolder(
              PdfFileSpecificationBaseHelper.getHelper(attachments[i])
                  .dictionary));
        }
        _helper.catalog.items![PdfName(PdfDictionaryProperties.af)] =
            fileAttachmentAssociationArray;
      }
    }
    await _helper.crossTable.saveAsync(writer);
    final DocumentSavedArgs argsSaved = DocumentSavedArgs(writer);
    await _onDocumentSavedAsync(argsSaved);
  }
  return writer.buffer!;
}