saveSync method

List<int> saveSync()

Saves the document and return the saved bytes as 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 = document.saveSync();
document.dispose();

Implementation

List<int> saveSync() {
  final List<int> buffer = <int>[];
  final PdfWriter writer = PdfWriter(buffer);
  writer.document = this;
  _checkPages();
  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 (_bookmark != null &&
      _bookmark!.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)))) {
      _copyOldStream(writer);
      if (_helper.catalog.changed!) {
        _readDocumentInfo();
      }
    } 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);
    }
    _appendDocument(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;
      }
    }
    _helper.crossTable.save(writer);
    final DocumentSavedArgs argsSaved = DocumentSavedArgs(writer);
    _onDocumentSaved(argsSaved);
  }
  return writer.buffer!;
}