Serving merged PDF to the browser using CAM::PDF library - perl

EDIT:
I changed my question again:
I am using this library to manipulate PDF files.
I am using this code to serve the output to the browser:
#!perl
use strict;
use warnings;
use lib "..\\mymodules\\CAM-PDF-1.57\\lib";
use CAM::PDF;
my $pdf = CAM::PDF->new('doc1.pdf');
# append the other file
my $anotherpdf = CAM::PDF->new('doc2.pdf');
$pdf->appendPDF($anotherpdf);
print "Content-Type: application/pdf\n";
print "Content-Disposition: inline\n\n";
print "Content-Transfer-Encoding: binary\n";
print "Accept-Ranges: bytes\n\n";
$pdf->output();
The result:
I get only the first pdf file loaded in the browser.
Problem solved:
I had to add $pdf->clean(); before the $pdf->output(); command, and it works perfect. :)

You said there is no TEMP variable, but your code using it:
$pdf->cleanoutput($ENV{"TEMP"} . '\\out1.pdf');
Try to set it to some value (I assuming that you are using windows)
$ENV{'TEMP'}='c:\tmp';
mkdir($ENV{'TEMP'});
die "$ENV{'TEMP'} not exists" if ! -d $ENV{'TEMP'};
$pdf->cleanoutput($ENV{"TEMP"} . '\\out1.pdf');
Why are you using // in some path? Like: use lib "..\mymodules\CAM-PDF-1.57\lib"; In use lib statement always use full path.

Related

get the whole content from the web site using perl script

I am a new hire in my company and first time I am working on Perl.
I get a task in which I find IP-Reputation from this link: https://www.talosintelligence.com/reputation_center/lookup?search=27.34.246.62
But in perl when we use:
#!/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
open FILE1, ">./Reports/Reputation.txt" or die "Cannot open Reputation.txt!";
my $mech = WWW::Mechanize->new( autocheck => 1 );
my $url="https://www.talosintelligence.com/reputation_center/lookup?search=27.34.246.62";
$mech->get($url);
print $mech->status();
my $content = $mech->content();
open FILE1, ">./Reports/Reputation.txt" or die "Cannot open Reputation.txt!";
print FILE1 ($content);
close FILE1;
print "\nIP Reputation Report Generated \n";
I don't get the whole content. What can I do to get this?
Contents are loading from JavaScript. So you can't crawl the content using simple methods.
There is two option for this kind of situation.
1) Some API contains the original data and JavaScript loads/formating the data in front end. If you want to parse the JavaScript loading content try to use the
WWW::Mechanize::Firefox
2) Try to figure out from where it is loading, for your IP following link has the corresponding data, which is JSON formated, so parse the content using JSON module. it is so simple compare to using RegEx.
https://www.talosintelligence.com/sb_api/query_lookup?query=%2Fapi%2Fv2%2Frelated_ips%2Fip%2F&query_entry=27.34.246.62

perl output image from a remote source

So I am trying to use a script to simply proxy some images through it. the images are hosted on a remote server so I need to download the image and display it. This is the code I have so far:
binmode STDOUT;
print "Content-type: image/jpeg\n";
#DB commands to find the image
my $file = $image_folder."/".$file_real."0000.jpg";
print $file;
my $html0 = get($file);
print $html0;
For some reason this does not work, when I change the header to html and print html0 it shows the data but when I change the header to jpeg or png it fails to compile!
Need two return characters in the header:
print "Content-type: image/jpeg\n\n";
Also, should be sure that the file is loaded in binmode as well.

i am searching for a that collect the text from web site and write it in to a single text file

i am searching for a perl script that takes website address as a input and collect the total text from given website and written to a single text file. so please do help me.
following is have currently
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
use CGI;
use Cwd;
$q=new CGI;
$a=$q->param('file');
chomp($a);
my $ftpname="$a";
system("wget --mirror -p --convert-links -x --reject=gif $ftpname");
Look at LWP or specifically at LWP::UserAgent. You can then write the received data to a file.

Open Excel file in perl and print row count

I am using Win32::OLE module to open an excel file and get row count. The problem is when i hard code excel file path it works fine but when i dynamically pass path it throw an error saying that "cant call method workbooks on unblessed reference". Please find the below sample code.
use OLE;
use Win32::OLE::Const 'Microsoft Excel';
my $xapp= Win32::OLE->GetActiveObject('Excel.Application')
or do { Win32::OLE->new('Excel.Application', 'Quit')};
$xapp->{'Visible'} = 0;
my $file='excel.xlsx';
my $fileName="c:/users/mujeeb/desktop/".$file;
print $fileName;
my $wkb = $xapp->Workbooks->Open($fileName); //here i am getting error coz i am passing dynamic fileName;
my $wks = $wkb->Worksheets('Sheet1');
my $Tot_Rows=$wks->UsedRange->Rows->{'Count'};
print $Tot_Rows."\n";
$xapp->close;
Use backslashes in the filename.
The filename is given to excel and excel won't understand forward slashes. Perl does not convert them because Perl doesn't know the string is a file.
Are you sure that there exists a method named as Open? Because I don't see it in the documentation of Win32::OLE. Also you must add use Win32::OLE; in your code.
You could use this line of code to change the path into readable path for OLE:
my $file='excel.xlsx';
my $fileName="c:/users/mujeeb/desktop/".$file;
$fileName=~s/[\/]/\\/g;
print $fileName;
outputs:
c:\\users\\mujeeb\\desktop\\excel.xlsx

CGI script cant create file

I have the following CGI script that launches a module that creates a PNG file and then shows it.
#!/usr/bin/perl
use RRDs;
use CGI;
main:
{
my $cgi = new CGI;
my $filename = $cgi->param('filename');
print "Content-type: text/html\n\n";
my $curr_time = time();
my $start_time = $curr_time-3600;
RRDs::graph("$filename", "--start", "$start_time", "DEF:DiskC=c.rrd:DiskC:AVERAGE", "AREA:DiskC#0000FF");
my $err = RRDs::error();
print "<HTML><HEAD><TITLE>Disk C Utilization</TITLE></HEAD><BODY><H1>Disk C Utilization</H1><BR>$err<img src=\"$filename\"/></BODY></HTML>";
}
The graph method says that can't create the PNG file. If I run this script in a command like it works fine so I think it's a matter of permissions. I already set chmod 755 on the cgi-script folder. What do you suggest? Is this related to Apache2 settings?
Um, check the logs :) CGI Help Guide
$filename is not the filename that you want to use , it can be anything the browser sends, even F:/bar/bar/bar/bar/bar/bar/bar/UHOH.png
Its unlikely that F:/bar/bar/bar/bar/bar/bar/bar/UHOH.png exists on your server
You want to generate a filename, maybe like this
sub WashFilename {
use File::Basename;
my $basename = basename( shift );
# untainted , only use a-z A-Z 0-9 and dot
$basename = join '', $basename =~ m/([.a-zA-Z0-9])/g;
# basename is now, hopefully, file.ext
## so to ensure uniqueness, we adulterate it :)
my $id = $$.'-'.time;
my( $file, $ext ) = split /\./, $basename, 2 ;
return join '.', grep defined, $file, $id, $ext;
} ## end sub WashFilename
You also want to place the file in a directory of your webserver that will serve images (NOT cgi-bin)
And you also want to use File::Type::WebImages to determine web image file types using magic and make sure the filename has the appropriate extension , so your server will send the appropriate headers
And don't forget to chmod the file appropriately
chmod 777 solved my problem!
Don't store a document to file unless you have a reason to: it's slow and introduces all kinds of potential hazards.
You don't appear to reuse the same image on different requests, which might be such a reason.
Instead, teach your CGI script to output the image directly, depending on how it is called (or write a second one that does).