http://qs321.pair.com?node_id=1212586


in reply to PDF API2 rect fill not displayed in android pdf viewer

There's no "Helvetica-Roman" in core, your code dies on me in both question and solution. The latter, while moves in right direction, produces white text below the black rectangle, are you sure it's what you want?

Because, each invocation of either "text" or "gfx" methods of page object appends a chunk of content to content array, and you still add text to 0-th content array element, regardless of this being in the end of your program.

To original problem: the PDF spec says, that inside "text objects" (fragments of content stream bracketed between BT and ET keywords) you can only use either text-related or general graphics state related operators. I.e., drawing graphics is not allowed while inside a "text object". Some viewers/interpreters may be more forgiving, but such PDF is still broken.

Just, add text to content object, returned by "text" method of a page. And add graphics to content object returned by "gfx" method of a page. Then the correct BT-ET frames will be handled for you automatically.

use strict; use warnings; use PDF::API2; my $pdf = PDF::API2-> new; my $page = $pdf-> page; my $font = $pdf-> corefont( 'Helvetica' ); my $content = $page-> text; $content-> font( $font, 16 ); $content-> fillcolor( '#000000' ); $content-> translate( 50, 620 ); $content-> text( 'Print some text' ); $content = $page-> gfx; $content-> rect( 50, 560, 400, 18 ); $content-> fillcolor( '#000000' ); $content-> fill; $content = $page-> text; $content-> font( $font, 16 ); $content-> fillcolor( '#ffffff' ); $content-> translate( 62, 565 ); $content-> text( 'Print text in rect' ); $pdf-> saveas( 'test.pdf' );

Replies are listed 'Best First'.
Re^2: PDF API2 rect fill not displayed in android pdf viewer
by Anonymous Monk on Apr 09, 2018 at 16:10 UTC
    Many thanks for your correction and your example code! Using $content makes so much sense.

    You make my day :)))