pdf-raku / PDF-API6

Facilitates the creation and modification of PDF files
Artistic License 2.0
6 stars 3 forks source link

README needs reorganization/transference to doco repository #11

Open dwarring opened 3 years ago

dwarring commented 3 years ago

This was created before https://github.com/pdf-raku/pdf-raku.github.io can into existence and has continued to grow and become a defacto documentation point for the rest of the tool-chain.

Initial problem is that it's in the wrong place. At some point the bulk of it should be moved to the doco repository and the README slimmed down to refer to it.

hectormonacci commented 2 years ago

The code example just after https://github.com/pdf-raku/PDF-API6/blob/master/README.md#to-xobject is intended to show how to copy and transform pages from $old to $pdf, but it fails to use $old after opening it.

I tried changing my PDF::XObject $xo = $pdf.page(2).to-xobject; to my PDF::XObject $xo = $old.page(2).to-xobject; but after this change the example won't run under rakudo v2022.06.

dwarring commented 2 years ago

@hectormonacci I fixed the to-xobject example as above. I was able to run it on 2020.06. What issues are you having?

hectormonacci commented 2 years ago

Thank you. Here's more info:

raku -v: Welcome to Rakudo™ v2022.06. Implementing the Raku® Programming Language v6.d. Built on MoarVM version 2022.06.

raku sample.raku (contains the example with minimal adaptations, and is applied to a valid v1.5 PDF input file):

indirect reference without associated reader: 3 0 R in method deref at /usr/lib/raku/site/sources/3BAE5B323236C0357B33115CDCDF3E54C11BBF2F (PDF::COS::Tie) line 252 in method AT-KEY at /usr/lib/raku/site/sources/4643BF545A394ED914BE56449B35DE1B11718B4C (PDF::COS::Tie::Hash) line 73 in method AT-KEY at /usr/lib/raku/site/sources/4643BF545A394ED914BE56449B35DE1B11718B4C (PDF::COS::Tie::Hash) line 71 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 27 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method ref-count at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 20 in method body at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 67 in method ast at /usr/lib/raku/site/sources/C3530A88CEE9B5F532C720CDF215667E67FE53CA (PDF::IO::Serializer) line 259 in method ast at /usr/lib/raku/site/sources/7418831955D69E9D1DF9DB769436C4CEFCD8A0C7 (PDF) line 191 in method ast-writer at /usr/lib/raku/site/sources/7418831955D69E9D1DF9DB769436C4CEFCD8A0C7 (PDF) line 196 in method save-as at /usr/lib/raku/site/sources/7418831955D69E9D1DF9DB769436C4CEFCD8A0C7 (PDF) line 232 in method save-as at /usr/lib/raku/site/sources/F51B91A5DBC668E9092938CAB20E1AD2D8E61375 (PDF::Class) line 57 in block <unit> at sample.raku line 45

dwarring commented 2 years ago

That's on line 45, but my code sample only has 19 lines. Can you please show the actual code? It would also be helpful to attach a sample PDF, if you are able.

hectormonacci commented 2 years ago

Sure thing!

1. My code:

#!/usr/bin/env raku

# use PDF::API6;
# use PDF::Page;
# use PDF::XObject;
# use PDF::Content;
# use PDF::Content::Page :PageSizes;

# my PDF::API6 $old .= open('1842-gogol-capote.pdf');
# my PDF::API6 $pdf .= new;
# my PDF::Page $page = $pdf.add-page();
# my PDF::Content $gfx = $page.gfx;

# # Import Page 2 from the old PDF
# my PDF::XObject $xo = $pdf.page(1).to-xobject;

# # Add it to the new PDF's first page at 1/2 scale
# my $width = $xo.width / 2;
# my $bottom = 5;
# my $left = 10;
# $gfx.do($xo, :position[$bottom, $left], :$width);

# $pdf.media-box = A4;
# $pdf.save-as('1842-gogol-capote-raku.pdf');

use PDF::API6;
use PDF::Page;
use PDF::XObject;
use PDF::Content;
my PDF::API6 $old .= open('1842-gogol-capote.pdf');
my PDF::API6 $pdf .= new;
my PDF::Page $page = $pdf.add-page;
my PDF::Content $gfx = $page.gfx;

# Import first page from the old PDF
my PDF::XObject $xo = $old.page(1).to-xobject;

# Add it to the new PDF's first page at 1/2 scale
my $width = $xo.width / 2;
my $bottom = 5;
my $left = 10;
$gfx.do($xo, :position[$bottom, $left], :$width);

$pdf.save-as('new.pdf');

2. The PDF file to be used as source:

1842-gogol-capote.pdf

dwarring commented 2 years ago

Thanks, I can replicate the issue.

That is very close to the example, apologies.

I'm thinking it must be somehow PDF-specific. Will investigate.

dwarring commented 2 years ago

I fixed a bug in PDF::Content::Page which implements the to-xobject method, which wasn't building the xobjects's resources correctly.

@hectormonacci could you please upgrade to PDF::Content 0.6.6 and try again.

hectormonacci commented 2 years ago

Great job @dwarring ! I upgraded to PDF::Content 0.6.6 and now it works. Thanks a lot!

hectormonacci commented 2 years ago

By the way, I think another change would be needed on the same example.

:position[$bottom, $left]

should be changed to:

:position[$left, $bottom]