From: kilian (dks-laptop) Date: Sat, 14 Dec 2019 04:57:10 +0000 (+0100) Subject: pdfcreator X-Git-Url: http://cloud.dks.lu/git/?a=commitdiff_plain;h=2005501254e21b260c33664d1334d85e77f80a33;p=dksnas.git pdfcreator --- diff --git a/bin/pdfcreator/createpdf.pl b/bin/pdfcreator/createpdf.pl index 76b1863..4316a80 100644 --- a/bin/pdfcreator/createpdf.pl +++ b/bin/pdfcreator/createpdf.pl @@ -21,17 +21,14 @@ my $outputfile = ""; my $pdfout = ""; my $outpath = ""; # my $datafile=dirname($0).'/pdftest.pdf'; -GetOptions ("data|d=s" => \$datafile, "template|t=s" =>\$templatefile,"outpath|o=s"=>\$outpath); +GetOptions ("template|t=s" =>\$templatefile,"outpath|o=s"=>\$pdfout); if (! -e $templatefile){ - print "file '$datafile' does not exist!\n"; + print "file '$templatefile' does not exist!\n"; exit(1); } -if (! -e $datafile){ - print "file '$datafile' does not exist!\n"; - exit(1); -} -if ($outpath eq "" || (! -d $outpath)){ - print "folder '$outpath' does not exist!\n"; + +if (! -d dirname($pdfout)){ + print "folder '".dirname($pdfout)."' does not exist!\n"; exit(1); } my $json = JSON::PP->new->utf8; @@ -42,37 +39,15 @@ while(my $l=){ $tmplstr .= $l; } close(TMPL); -my $datastr; -open(DATA,$datafile); -while(my $l=){ - $datastr .= $l; -} -close(DATA); - -my $data = $json->decode($datastr); -foreach my $d (keys(%{$data})){ - #print $d."=>".ref($data->{$d})."=>".encode("utf-8",$data->{$d})."\n"; - if (ref($data->{$d}) eq ""){ - my $search = '%%'.uc($d).'%%'; - my $replace = encode("utf-8",$data->{$d}); - $tmplstr =~ s/$search/$replace/g; - } else { - my $search = '\[\"%%'.uc($d).'%%\"\]'; - my $replace = encode("utf-8",join("",@{$data->{$d}})); - #print Dumper(); - $tmplstr =~ s/$search/$replace/g; - } - -} -#print Dumper($tmplstr); -$tmpl = $json->decode($tmplstr); -#print Dumper($tmpl->{sections}); + +$tmpl = $json->decode($tmplstr); +#print Dumper($tmpl); #$pdf->preferences({-fitwindow => 1}); -my $page = $pdf->page(); +# my $page = $pdf->page(); #$page->mediabox('A4'); if (exists($tmpl->{global})){ @@ -83,13 +58,13 @@ if (exists($tmpl->{global})){ 'Author' => "DKS InvoiceJournal", 'Creator' => "CreatePDF", 'Producer' => "CreatePDF", - 'Title' => $data->{title}, - 'Subject' => $data->{subject}, + 'Title' => $tmpl->{global}->{title}, + 'Subject' => $tmpl->{global}->{subject}, 'Keywords' => "" ); - $pdfout = $outpath.'/'.substr(basename($datafile),0,-5).".pdf"; - if (-e $outpath.'/'.substr(basename($datafile),0,-5).".pdf"){ - unlink($outpath.'/'.substr(basename($datafile),0,-5).".pdf"); + + if (-e $pdfout){ + unlink($pdfout); } } else { print "No global Options!\n"; @@ -97,25 +72,23 @@ if (exists($tmpl->{global})){ } - -foreach my $s (@{$tmpl->{sections}}){ - - $endpoints->{$s} = {final_y => '', lastpage => 1,pages => 1}; - if ($s->{type} eq "image"){ -# #print "Add Image $s\n"; - $s->{imgpath} = dirname($templatefile); - $endpoints->{$s->{name}}->{y} = &addimage($pdf,$page,$s); - } elsif ($s->{type} eq "line"){ -# #print "Add Line $s\n"; - $endpoints->{$s->{name}}->{y} = &addline($page,$s); - } elsif ($s->{type} eq "text"){ -# #print "Add Text $s\n"; - $endpoints->{$s->{name}}->{y} = &addtext($pdf,$page,$s); - } elsif ($s->{type} eq "table"){ - my $xname=$s->{name}; - ($endpoints->{$xname}->{lastpage}, $endpoints->{$xname}->{pages}, $endpoints->{$xname}->{y}) = &addtable($pdf,$page,$s); +my $page = $pdf->page(); +foreach my $s (@{$tmpl->{content}}){ + &printsection($pdf,$page,$s); +} +my $numpages = $pdf->pages; +foreach (my $p=1;$p<=$numpages;$p++){ + $page = $pdf->openpage($p); + if (exists($tmpl->{header})){ + foreach my $x (@{$tmpl->{header}}){ + &printsection($pdf,$page,$x); + } + } + if (exists($tmpl->{footer})){ + foreach my $x (@{$tmpl->{footer}}){ + &printsection($pdf,$page,$x); + } } - } $pdf->saveas($pdfout); @@ -123,19 +96,49 @@ $pdf->end; #Functions +sub printsection(){ + my $pdf = shift; + my $page = shift; + my $data = shift; + my $start_y = shift; + my $y = undef; + if (defined($start_y)){ + $y = $start_y; + } + if (exists($data->{image})){ + $data->{image}->{imagepath} = dirname($templatefile); + $y = &addimage($pdf,$page,$data->{image}); + } elsif (exists($data->{line})){ + $y = &addline($page,$data->{line}); + } elsif (exists($data->{text})){ + $y = &addtext($pdf,$page,$data->{text}); + } elsif (exists($data->{table})){ + print"has table\n"; + $y = &addtable($pdf,$page,$data->{table}); + } + print "last y: ".$y."\n"; + return $y; +} + sub addimage(){ my $pdf = shift; my $page = shift; my $section = shift; + print Dumper($section); my $gfx = $page->gfx(); - my ($iw, $ih) = imgsize($section->{imgpath}.'/'.$section->{src}); + my ($iw, $ih) = imgsize($section->{imagepath}.'/'.$section->{src}); my $nimgw = $section->{width}; my $nimgh = ($nimgw/$iw) * $ih; #print "New Imagesize: w:".$nimgw." h:".$nimgh."\n"; + print "Image: x:".$section->{x}."\n"; + print "Image: y:".$section->{y}."\n"; $gfx->translate($section->{x},$section->{y}); - my $img = $pdf->image_png($section->{imgpath}.'/'.$section->{src}); + my $img = $pdf->image_png($section->{imagepath}.'/'.$section->{src}); $gfx->image($img,0,0,$nimgw,$nimgh); - $gfx->translate(0,0); + my $tx = int($section->{x}) * -1; + my $ty = int($section->{y}) * -1; + print "translate to: x:".$tx." y:".$ty."\n"; + $gfx->translate($tx,$ty); $gfx->save; return $nimgh + $section->{y}; } @@ -184,52 +187,74 @@ sub addtable(){ my $pdf = shift; my $page = shift; my $section = shift; - my $data = $section->{rows}; - my $pdftbl = new PDF::Table; - delete($section->{type}); - delete($section->{rows}); - delete($section->{name}); - delete($section->{option}); + + my $hx = $section->{x}; + my $hy = $section->{y}; - if (exists($section->{start_y})){ - if ($section->{start_y} =~ /^final_y/){ - my ($sec) = $section->{start_y} =~ /^final_y:(.+)$/; - print "$sec\n"; - - $section->{start_y} = $endpoints->{$sec}->{y}; - + if (exists($section->{header})){ + for (my $h=0;$h{col_def}});$h++){ + my $hdefcol = $section->{header_def}[$h]; + $hdefcol->{x} = $hx; + $hdefcol->{y} = $hy; + $hdefcol->{text} = $section->{header}[$h]->{text}; + print "$h:".Dumper($hdefcol); + &addtext($pdf,$page,$hdefcol); + $hx += $section->{col_def}[$h]->{width}; } } - if (exists($section->{font})){ - $section->{font} = $pdf->corefont($section->{font}); - } - - #print "HeadProps\n"; - if (exists($section->{header_props})){ - foreach (my $i=0;$i{header_props}});$i++){ - my $tmpdata = @{$section->{header_props}}[$i]; - if (exists($tmpdata->{font})){ - $tmpdata->{font} = $pdf->corefont($tmpdata->{font}); - } - @{$section->{header_props}}[$i] = $tmpdata; - } - } - #print "ColProps\n"; - if (exists($section->{column_props})){ - foreach (my $i=0;$i{column_props}});$i++){ - my $tmpdata = @{$section->{column_props}}[$i]; - if (exists($tmpdata->{font})){ - $tmpdata->{font} = $pdf->corefont($tmpdata->{font}); - } - @{$section->{column_props}}[$i] = $tmpdata; - } - } - #print Dumper($section); - #print ref %{$section}."\n"; - my ($lastpage, $tblpages, $final_y) = $pdftbl->table($pdf,$page, $data, %{$section}); - return ($lastpage, $tblpages, $final_y); + return $hy; } +# sub addtable(){ +# my $pdf = shift; +# my $page = shift; +# my $section = shift; +# my $data = $section->{rows}; +# my $pdftbl = new PDF::Table; +# delete($section->{type}); +# delete($section->{rows}); +# delete($section->{name}); +# delete($section->{option}); + +# if (exists($section->{start_y})){ +# if ($section->{start_y} =~ /^final_y/){ +# my ($sec) = $section->{start_y} =~ /^final_y:(.+)$/; +# print "$sec\n"; + +# $section->{start_y} = $endpoints->{$sec}->{y}; + +# } +# } +# if (exists($section->{font})){ +# $section->{font} = $pdf->corefont($section->{font}); +# } + +# #print "HeadProps\n"; +# if (exists($section->{header_props})){ +# foreach (my $i=0;$i{header_props}});$i++){ +# my $tmpdata = @{$section->{header_props}}[$i]; +# if (exists($tmpdata->{font})){ +# $tmpdata->{font} = $pdf->corefont($tmpdata->{font}); +# } +# @{$section->{header_props}}[$i] = $tmpdata; +# } +# } +# #print "ColProps\n"; +# if (exists($section->{column_props})){ +# foreach (my $i=0;$i{column_props}});$i++){ +# my $tmpdata = @{$section->{column_props}}[$i]; +# if (exists($tmpdata->{font})){ +# $tmpdata->{font} = $pdf->corefont($tmpdata->{font}); +# } +# @{$section->{column_props}}[$i] = $tmpdata; +# } +# } +# #print Dumper($section); +# #print ref %{$section}."\n"; +# my ($lastpage, $tblpages, $final_y) = $pdftbl->table($pdf,$page, $data, %{$section}); +# return ($lastpage, $tblpages, $final_y); +# } + diff --git a/bin/pdfcreator/output/testnew.pdf b/bin/pdfcreator/output/testnew.pdf new file mode 100644 index 0000000..17d729f Binary files /dev/null and b/bin/pdfcreator/output/testnew.pdf differ diff --git a/bin/pdfcreator/templates/dks_new.json b/bin/pdfcreator/templates/dks_new.json new file mode 100644 index 0000000..713e880 --- /dev/null +++ b/bin/pdfcreator/templates/dks_new.json @@ -0,0 +1,50 @@ +{"global": { + "title":"TEST DOC", + "subject":"TEST DOC", + "preferences": { "fitwindow": 1 }, + "orientation": "portrait", + "size": "A4" +}, +"header":[ + + {"line": { "color": "black", "width": 1, "start": { "x": 50, "y": 753 }, "end": { "x": 285, "y": 753 }}}, + {"text": {"align": "center","font": { "name": "Helvetica", "size": 9 },"x": 200,"y": 743, "data": "Database Knowledge Solutions - Simplify IT!"}}, + {"text": {"align": "right", "font": { "name": "Helvetica", "size": 11 }, "x": 550, "y": 820, "lineheight": 14, "text": "DKS s.à r.l.\n8b, rue du Moulin\n6914 Roodt/Syre\n\nTel: +352 691 504574\ninfo@dks.lu / www.dks.lu"}}, + {"text":{"align": "center","font": { "name": "Helvetica", "size": 9 },"x": 200,"y": 743,"text": "Database Knowledge Solutions - Simplify IT!"}}, + {"image":{ "x": 50,"y": 755,"width": 160, "src": "dks_1000.png"}} +], +"footer":[ {"text": + { + "align": "center", "font": { "name": "Helvetica", "size": 10 }, "x": 297.5, "y": 50, "lineheight": 15, "text": "DKS, Société à responsabilité limitée, RC Luxembourg,B168572 - TVA: LU 2537 5617 - No. Aut: 10024550 / 0\nIBAN: LU25 0020 1100 2783 8700; BIC: BILLLULL" +}} +], +"content":[ + { "table":{ "x":"20","y":"600", + "col_def":[{"width": 50},{"width": 50},{"width": 50},{"width": 50}], + "header_def":[ + {"align": "left", "font": { "name": "Helvetica", "size": 11 }, "lineheight": 14}, + {"align": "left", "font": { "name": "Helvetica", "size": 11 }, "lineheight": 14}, + {"align": "right", "font": { "name": "Helvetica", "size": 11 }, "lineheight": 14}, + {"align": "right", "font": { "name": "Helvetica", "size": 11 }, "lineheight": 14} + ], + "row_def":[ + {"align": "left", "font": { "name": "Helvetica", "size": 9 }, "lineheight": 11}, + {"align": "left", "font": { "name": "Helvetica", "size": 9 }, "lineheight": 11}, + {"align": "right", "font": { "name": "Helvetica", "size": 9 }, "lineheight": 11}, + {"align": "right", "font": { "name": "Helvetica", "size": 9 }, "lineheight": 11} + ], + "header":[ + {"text": "Head 1"}, + {"text": "Head 2"}, + {"text": "Head 3"}, + {"text": "Head 4"} + ], + "rows":[ + [{"text": "Row 01"},{"text": "Row 02"},{"text": "Row 03"},{"text": "Row 04"}], + [{"text": "Row 11"},{"text": "Row 12"},{"text": "Row 13"},{"text": "Row 14"}], + [{"text": "Row 21"},{"text": "Row 22"},{"text": "Row 23"},{"text": "Row 24"}], + [{"text": "Row 31"},{"text": "Row 32"},{"text": "Row 33"},{"text": "Row 34"}] + ] + }} +] +} \ No newline at end of file