Wondering how I can store FPDF generated file from form fields in a variable or other for use in CURL request. I am able to upload a file via the file upload e.g input type="file" name="file" manually and post it with CURL but not with the automatically generated pdf file.
Is this possible? See below code:
$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);
//FPDF
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(0,100, "Note Title: {$noteTitle} \n", 1, 0, 'C');
$pdf->Cell(0,100, "Note Body: {$noteBody}", 1, 0, 'C');
$filename="test.pdf";
$pdf->output($filename,'f');
$cfile = new CURLFILE($_FILES[$pdf]['tmp_name'], $_FILES[$pdf]['type'], $_FILES[$pdf]['name']);
//Post to curl
$data = array();
$data['FILE_ATTACHMENTS'] = $cfile;
//curl code etc
...
First you need to save dynamically generated at server directory then you can use for the upload or any other purpose like send as attachment email.
For Saving the PDF as a file use below method:
$pdf->Output('yourfilename.pdf','F');
If you want to save file at server directory:(make sure you have 777 writing permissions for that folder!):
$pdf->Output('directory/yourfilename.pdf','F');
For more details you can visit tutorial: http://www.phpmysqlquery.com/fpdf-output-methods-for-pdf-files-in-php/
Related
I am trying to get a document generated on a php site. It is all working well but the last thing is to add a header to the top of every page after the title page. I have tried so many things and the closest I can get to what I want is the following (stripped down) code.
ob_start();
$mpdf = new Mpdf();
$mpdf->SetDisplayMode('fullpage');
$mpdf->bleedMargin = 0;
$mdpf->crossMarkMargin = 0;
$mpdf->cropMarkMargin = 0;
$mpdf->nonPrintMargin = 0;
// Home page and headers...
$html_header = 'Some Code here';
$mpdf->DefHTMLHeaderByName('PgHeader', $html_header);
$mpdf->AddPage();
$mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);
// Get the content of any pages we have
if($pdf_pages) {
foreach($pdf_pages as $key=>$pg) {
$mpdf->AddPageByArray(array(
'mgt' => '40',
'odd-header-name' => 'PgHeader'
));
$html .= 'the content from the loop';
// Write some HTML code:
$custom_sheet = file_get_contents('/custom.css');
$mpdf->WriteHTML($custom_sheet, 1);
$mpdf->WriteHTML($html, 2);
}
}
// Output a PDF file directly to the browser
$mpdf->Output('Name.pdf', 'I');
ob_end_flush();
I have tried changing the odd-header-name to html_PgHeader based on the documentation but in either case, I get the margin, all the content but the header never shows up. Why? Why is it not being assigned so I can call it in the AddPageByArray?
According to the documentation you need to prefix header/footer names with 'html_'
Try changing;
'odd-header-name' => 'PgHeader',
to;
'odd-header-name' => 'html_PgHeader',
reference here
To show header, you need to add odd-header-value option to AddPageByArray method with value 1.
ob_start();
$mpdf = new Mpdf();
$mpdf->SetDisplayMode('fullpage');
$mpdf->bleedMargin = 0;
$mdpf->crossMarkMargin = 0;
$mpdf->cropMarkMargin = 0;
$mpdf->nonPrintMargin = 0;
// Home page and headers...
$html_header = 'Some Code here';
$mpdf->DefHTMLHeaderByName('PgHeader', $html_header);
$mpdf->AddPage();
$mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);
$pdf_pages = array('key' => 'val');
// Get the content of any pages we have
if($pdf_pages) {
foreach($pdf_pages as $key=>$pg) {
$mpdf->AddPageByArray(array(
'mgt' => '40',
'odd-header-name' => 'PgHeader',
'odd-header-value' => 1,
));
$html .= 'the content from the loop';
// Write some HTML code:
$custom_sheet = file_get_contents('/custom.css');
$mpdf->WriteHTML($custom_sheet, 1);
$mpdf->WriteHTML($html, 2);
}
}
// Output a PDF file directly to the browser
$mpdf->Output('Name.pdf', 'I');
ob_end_flush();
Pt.II
If this, don't help, then try this scenario:
Open console and go to site's root path.
Create index.php file
Run composer require mpdf/mpdf (install composer, if you don't have it).
Paste this code to the index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
ob_start();
$mpdf = new \Mpdf\Mpdf();
/** same code from previous example here, except Mpdf init **/
Try something like this
#page {
header: header_name;
footer: footer_name;
}
It might help you.
Url: https://mpdf.github.io/headers-footers/headers-footers.html
im trying to download a remote xml file, but is not working, is not storing the file on my storage.
my code:
$url = 'http://xml.url.xml';
set_time_limit(0);
// Download file and save it on folder
$guzzleClient = new Client();
$response = $guzzleClient->get($url);
$body = $response->getBody();
$body->seek(0);
$size = $body->getSize();
$file = $body->read($size);
Storage::download($file);
The Storage::download() method is used to generate response, that will force the download in the browser.
Use Storage::put('filename.xml', $content) instead.
You can read more in the docs:
https://laravel.com/docs/5.6/filesystem
Scenario :
I have a target website that I need to crawl and take a screenshot of the personal account feed.
Needs :
Login to the website.
Browse to the personal area.
crawl the page.
Code :
require 'vendor/autoload.php';
use JonnyW\PhantomJs\Client;
$client = Client::getInstance();
$client->getEngine()->setPath('C:\xampp\htdocs\phantomjs\bin\phantomjs.exe');
$client->getProcedureCompiler()->clearCache();
$client->isLazy();
$delay = 15; // 5 seconds
$width = 1366;
$height = 768;
$top = 0;
$left = 0;
$request = $client->getMessageFactory()->createCaptureRequest();
$response = $client->getMessageFactory()->createResponse();
$request->setDelay($delay);
$request->setTimeout(10000);
$data = array(
'login' => '***',
'password' => '***',
);
$request->setMethod('POST');
$request->setUrl('login-url');
$request->setRequestData($data); // Set post data
$request->setOutputFile('screenshot.jpg');
$request->setViewportSize($width, $height);
$request->setCaptureDimensions($width, $height, $top, $left);
$client->send($request, $response);
$file = fopen("1.txt","a");
fwrite($file,$response->getContent());
fclose($file);
Question :
How to browse to the personal page URL without loosing the cookies and the session ?
I have already tried to only change the setUrl another time on the same request, but it's not working.
$request->setMethod('GET');
$request->setUrl('personal-page-url');
$request->setOutputFile('screenshot1.jpg');
$client->send($request, $response);
$file = fopen("2.txt","a");
fwrite($file,$response->getContent());
fclose($file);
According to this issue on github
, there is still an unfixed problem with cookies. you can follow it.
Cookie and php-phantomjs #124 Open lucl22 opened this issue on Oct 1,
2016 ยท 3 comments
Or you can use other ways of scrapping if your target webpage doesn't have so much ajax data transmission like:
Goutte
httpful
guzzle
php-vcr
if you realy need js to be run you can use other web-drivers for php
how-to-use-selenium-with-php
php-webdriver
I am using HTML2pDf convertor to generate pdf my code looks like this
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
$html2pdf->Output($username.'_questionnaire.pdf');
I am getting following error
TCPDF ERROR: Some data has already been output, can't send PDF file
The function where I have this script looks like this
public function postExporttopdf(){
$validator = Validator::make(Input::all(),array('delid'=>'required'));
$event = Session::get('tempevenname');
if($validator->passes()){
$uid1 = Input::get('delid');
$username = User::find(Input::get('delid'))->name;
$page1data = Question::where('event','=',$event)->where('page','=',1)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page2data = Question::where('event','=',$event)->where('page','=',2)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page3data = Question::where('event','=',$event)->where('page','=',3)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page4data = Question::where('event','=',$event)->where('page','=',4)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$html22 = View::make('reviewsendpdf')->with(array(
'page1data'=>$page1data,
'page2data'=>$page2data,
'page3data'=>$page3data,
'page4data'=>$page4data
));
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
$html2pdf->Output($username.'_questionnaire.pdf');
} else {
return Redirect::to('admin/exporttopdf')->with('message','Select a user and event');
}
}
Probably something has already been written to the output buffer prior to streaming the pdf content.
Try to use ob_end_clean() to clean the output buffer just before the method invocation:
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
ob_end_clean();
$html2pdf->Output($username.'_questionnaire.pdf');
I have an html file named welcomemailtemplate.html and I need to convert that file to a PDF.
I first read this file using the following method provided by Yii framework:
$filename = Yii::app()->basePath.'\views\email\welcomemailtemplate.html';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$name = $model->name;
fclose($handle);
$message = str_replace ( "[username]", $name, $contents );
Then, to generate the PDF file, the following parameters are set:
Yii::import('application.vendors.*');
require_once('tcpdf/tcpdf.php');
require_once('tcpdf/config/lang/eng.php');
$pdf = new TCPDF();
$pdf->SetCreator("Torget");
$pdf->SetAuthor('test name');
$pdf->SetTitle('Savani Test');
$pdf->SetSubject(' Torget Order Confirmation');
$pdf->SetKeywords(' Torget, Order, Confirmation');
//$pdf->SetHeaderData('', 0, PDF_HEADER_TITLE, '');
$pdf->SetHeaderData('', 0, "Torget Order", '');
$pdf->setHeaderFont(Array('helvetica', '', 8));
$pdf->setFooterFont(Array('helvetica', '', 6));
$pdf->SetMargins(15, 18, 15);
$pdf->SetHeaderMargin(5);
$pdf->SetFooterMargin(10);
$pdf->SetAutoPageBreak(TRUE, 0);
$pdf->SetFont('dejavusans', '', 7);
$pdf->AddPage();
If I pass the content as follows, it creates the PDF:
$pdf->writeHTML("<span>Hello World!</span>", true, false, true, false, '');
But if I pass the read html file content for pdf creating using following method it gives following error:
$pdf->writeHTML($message, true, false, true, false, '');
$pdf->LastPage();
Error message:
Undefined index: thead
Try to validate the file using the w3c validator http://validator.w3.org/.
I've worked with tcpdf before but i gave it up because it didn't seem reliable. You can also try wkhtmltopdf binary (only if your hosting allows you to use proc_open/proc_close). Seems a little more stable to me. It also has a PHP class to help you use it.
CutyCapt seems to be a very good option for you. Its very easy to integrate also.