I have created a PDF using PDF::API2. How can I return the raw PDF?
Example:
my $pdf = PDF::API2->new();
$pdf = PDF::API2->open('./useful_scripts/invoice.pdf');
my $page = $pdf->openpage(1);
my $content = $page->text();
my $font = $pdf->corefont('Helvetica-Bold');
$content->fillcolor('black');
$content->font($font, 11);
$content->translate(170, 785);
$content->text($invoice->ott_invoice_number);
#Finally return raw $pdf
I am aware of the $pdf->saveas() method, but I don't actually want to save my PDF to a file.
IRC #perl-help answer:
my $raw_pdf = $pdf->stringify();
does the job.
Related
I have created a form in drupal 7 and it has a field to upload file (csv file only ) now how to display uploaded csv file into table on form submit ?
Not sure about theme function, but you can do it on your own.
I.e. use:
http://php.net/manual/en/function.file-get-contents.php
To read the file and then:
http://php.net/manual/en/function.str-getcsv.php
to parse CSV.
Or, maybe:
http://php.net/manual/en/function.fgetcsv.php
to read row by row. Anyway, you'll end up with looping trough rows so just print values the way you want, add markup around the values...
After go through various code exercises i have come up with solution which is as follows :
`function display_table($filename, $head=false) {
$handle = fopen($filename, "r");
$all_rows = array();
$header = null;
while ($row = fgetcsv($handle)) {
if ($header === null) {
$header = $row;
continue;
}
$all_rows[] = array_combine($header, $row);
}
$table = theme('table', array('header' => $header, 'rows' => $all_rows));
return $table;
}`
Hope it would be helpful to others as well !
i got this code in attaching a pdf file to my site in order for the viewers to download such pdf file..
$fullPath = "../public/pdffiles/FolioPlusUserGuide(v3.0).pdf";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
html code:
baseUrl().'/DownloadPdf'?>">DOWNLOAD BROCHURE
this code only accommodates 1 file path, I have more pdf files with different filename and different buttons for each to handle the download event. how can i achieve this?..thanks ahead!..=)
You can pass PDF file name via HTTP request, like this:
DownloadPDF.php?filename=test1.pdf
Then in your DownloadPDF file have something like this:
$pdfdir = "/pdffiles/folder/location/here";
$pdffilename = $_GET['filename'];
$fullPath = $pdfdir.$pdffilename;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
Now your code is dynamic and you can download any PDF with links like this:
<a href=downloadpdf.php?filename=something.pdf>Something</a>
<a href=downloadpdf.php?filename=something2.pdf>Something2</a>
etc.
I am trying to upload a file from a web page. I use CGI to query for all of the input fields but "my $upload_filehandle = $query->upload("fileInput");" is always empty even when "my $file = $query->param("fileInput");" has properly fetched the file name from the same field. Here is the code:
use CGI;
$CGI::POST_MAX = 1024 * 5000;
my $query = CGI->new;
my $url = $query->param("urlInput");
my $file = $query->param("fileInput");
my $upload_filehandle = $query->upload("fileInput");
my $text = $query->param("textInput");
my $k = $query->param("kInput");
Any advice is appreciated.
Regards.
I will repost my comment, as an answer:
make sure your HTML form contains the enctype attribute, and that it's set to enctype="multipart/form-data"
I have been tinkering around with PDF::API2 and i am facing a problem, create a pdf file very well and add text into it. However say if the text to be written flows over to more than one page, the script does not print over to the next page. I have tried researching for an answer to this but to no avail. I would like each page to have exactly 50 lines of text. My script is as below. It only prints on the first page, creates the other pages but does not print into them. Anyone with a solution
!/usr/bin/perl
use PDF::API2;
use POSIX qw(setsid strftime);
my $filename = scalar(strftime('%F', localtime));
my $pdf = PDF::API2->new(-file => "$filename.pdf");
$pdf->mediabox(595,842);
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;
$txt->font($fnt, 20);
$txt->translate(100,800);
$txt->text("Lines for $filename");
my $i=0;
my $line = 780;
while($i<310)
{
if(($i%50) == 0)
{
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
}
$txt->font($fnt, 10);
$txt->translate(100,$line);
$txt->text("$i This is the first line");
$line=$line-15;
$i++;
}
$txt->textend;
$pdf->save;
$pdf->end( );
The problem is that you are making new page, but forget new variables instantly:
if(($i%50) == 0)
{
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
}
All my variables you make disappear on closing parentheses. Just remove my and you will modify variables from top-level scope.
Edit: You also probably want to reset $line variable when making new page.
The typeface, $fnt, does not have to be changed since it depends on the PDF, $pdf, and not the page, $page.
As much as I love Perl, I learned enough Python to use the ReportLabs library for PDF generation. Creating PDF is one of the weak spots of Perl v. Python.
<?php
$title = $_POST['title'];
$filename = $title , ".php";
$fh = fopen($filename, 'w') or die ("can't open file");
$stringData = $title;
fwrite($fh, $stringData);
$stringData = $blog;
fwrite($fh, $stringData);
fclose($fh);
?>
This is only a sample. What is the correct code for that?
You are using the correct code there, what is the point?
Also note that you are using a comma rather than a dot to concatenate strings at:
$filename = $title , ".php";
In your example opening a file using POST is an insecure method, so don't even think about this kinda tricks :P
You can use file read and write in simple method
file_get_contents();
echo $fileData = file_get_contents('filename.txt');
file_put_contents();
$data= 'some data';
// Write the contents back to the file
file_put_contents("filename.txt", $data);