#!/usr/bin/perl -w use strict; use Text::Wrap; use PDF::Create; use CGI qw(:standard); use DBI(); CGI::ReadParse(); #define how many columns text to be wrapped to $Text::Wrap::columns = 58; #database stuff to get my text my $dbh = DBI->connect("DBI:mysql:database=somedb;host=xyz.net", "username", "password", {'RaiseError' => 1}); my $text_id = param('iid'); my $textfinal; my $retrieve = $dbh->prepare("SELECT text FROM texts where id=$text_id"); $retrieve->execute(); while (my $ref = $retrieve->fetchrow_hashref()) { $textfinal = $ref->{'text'}; } $retrieve->finish(); # Disconnect from the database. $dbh->disconnect(); #pdf stuff to make my pdf my $pdf = new PDF::Create('filename' => '/Users/username/Desktop/pagetest.pdf', 'Version' => 1.2, 'PageMode' => 'UseNone', 'Author' => 'www.xyz.net', 'Title' => 'Some Document', ); my $root = $pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]); # Add a page which inherits its attributes from $root my $page = $root->new_page; # Prepare 2 fonts my $font1 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica'); my $font2 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Courier'); #some text for the page my $line1 = 'some text here'; my $line2 = 'some text there'; my $footer = 'www.xyz.net'; #for text::wrap to format it needs these values my $initial_tab = "\t"; # Tab before first line my $subsequent_tab = ""; # All other lines flush left #split the original text into an array #this is where the error comes up #Use of uninitialized value in split at filename.pl line XX. #However when I have this in another perl script it works just fine my @text = split(/\n/, $textfinal); #indent all paragraphs not just the first my $newtext = fill($initial_tab, $subsequent_tab, @text); #now split the formatted text into lines so I can print them onto the pdf my @page_text = split(/\n/, $newtext); #other stuff like page number, formatting, etc. my $pageNo = 1; $page->string($font2, 10, 530, 45, $pageNo++); $page->stringc($font2, 10, 306, 720, $line1); $page->stringc($font2, 10, 306, 708, $line2); $page->stringc($font2, 10, 306, 45, $footer); my $lineCountNo = 1; my $height = 675; my $lineCount = 1; foreach my $new_line(@page_text) { #this is where the formatted text comes in one line at a time for the pdf $page->string($font2, 10, 95, $height, $new_line); $height -= 23; $lineCount++; } # finish the pdf $pdf->close;