output method

Future<void> output(
  1. PdfStream os, {
  2. bool enableEventLoopBalancing = false,
})

Writes the document to an output stream.

Implementation

Future<void> output(
  PdfStream os, {
  bool enableEventLoopBalancing = false,
}) async {
  PdfSignature? signature;

  final xref = PdfXrefTable(lastObjectId: _objser);

  for (final ob in objects.where((e) => e.inUse)) {
    ob.prepare();
    if (ob is PdfInfo) {
      xref.params[PdfNameTokens.info] = ob.ref();
    } else if (ob is PdfEncryption) {
      xref.params[PdfNameTokens.encrypt] = ob.ref();
    } else if (ob is PdfSignature) {
      assert(signature == null, 'Only one document signature is allowed');
      signature = ob;
    }
    xref.objects.add(ob);
  }

  final id =
      PdfString(documentID, format: PdfStringFormat.binary, encrypted: false);
  xref.params[PdfNameTokens.id] = PdfArray([id, id]);

  if (prev != null) {
    xref.params[PdfNameTokens.prev] = PdfNum(prev!.xrefOffset);
  }

  if (enableEventLoopBalancing) {
    await xref.outputAsync(catalog, os);
  } else {
    xref.output(catalog, os);
  }

  if (signature != null) {
    await signature.writeSignature(os);
  }
}