### ----------------------------------------------------
### -- PDF
### -- Licenses: Artistic-2.0
### -- Authors: David Warring
### -- File: lib/PDF/IO/Reader.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 2
    my PDF::IO::Serializer $serializer .= new: :$reader;

    my Array $body = $rebuild
        ?? $serializer.body( $reader.trailer, |c )
        !! $serializer.body( |c );

    .crypt-ast('body', $body, :mode<encrypt>)
        with $reader.crypt;
    :cos{
        :header{ :$.type, :$!version },
        :$body,
    }
}

#| dump to json
multi method save-as( Str $output-path where m:i/'.json' $/, |c ) {
    my \ast = $.ast(|c);
    $output-path.IO.spurt( to-json( ast ) );
}

#| write to PDF/FDF
multi method save-as(IO() $output-path, :$stream, |c ) {
    my $ast = $.ast(:!eager, |c);
    my PDF::IO::Writer $writer .= new: :$!input, :$ast, :$.compat;
    if $stream {
        my $ioh = $output-path.open(:w, :bin);
        $writer.stream-cos: $ioh, $ast<cos>;
        $ioh.close;
    }
    else {
        $output-path.spurt: $writer.Blob;
    }
    $writer;
}



