Converting html to pdf after clicking on download button in codeigniter - codeigniter

I have created a resume builder application.User enters all required details and i am storing all details in database. Then i am fetching those records and displaying it in different templates. I have created 3 templates. Now I want to give download as pdf and download as docx two download buttons. Once user clicks on any of buttons file will be downloaded in their chosen format. Either pdf or docx.
Is it possible to achieve in codeigniter?
i used Tcpdf
Controller Code is
the code written in controller download $this - > load - > library("Pdf");
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set auto page breaks
$pdf - > SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf - > setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf - > AddPage();
// Set some content to print
$html = $this - > load - > view('resume/res_template1_view', array('left_sidebar' => 'sidebar/left_sidebar', ' right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
// Print text using writeHTMLCell()
$pdf - > writeHTML($html, 0, 0, '', '', 0, 1, 0, true, '', true);
//$pdf->writeHTML($html, true, false, true, false, '');
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf - > Output('resume.pdf', 'D');

Probably you need to use some library in CI to achieve this. Please check below.
you can use Html2Pdf for CI3 library. https://github.com/shyamshingadiya/HTML2PDF-CI3
How to Use
There are 4 options you a required to set in order to create a PDF; folder, filename, paper and HTML
//Load the library
$this->load->library('html2pdf');
//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');
//Set the filename to save/download as
$this->html2pdf->filename('test.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
//Load html view
$this->html2pdf->html(<h1>Some Title</h1><p>Some content in here</p>);
The create function has two options 'save' and 'download'. Save will just write the PDF to the folder you choose in the settings. Download will force the PDF to download in the clients browser.
//Create the PDF
$this->html2pdf->create('save');
Advanced usage
Theres no reason why you can't build and pass a view to the html() function. Also note the create() function will return the saved path if you choose the 'save' option.
$data = array(
'title' => 'PDF Created',
'message' => 'Hello World!'
);
//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));
if($path = $this->html2pdf->create('save')) {
//PDF was successfully saved or downloaded
echo 'PDF saved to: ' . $path;
}
Please check above mentioned solution. Let me know if it not works.

Related

How can I download pdf before loading another view in laravel?

after completing a registration formular, when user clicks submit button, I want to first download a pdf and then redirect user to a view. Here is my code :
public function formularSave(Request $request) {
if(!isset($_REQUEST['token'])) {
abort(404);
}
$token = $_REQUEST['token'];
$upd_app = Application::where('token', $token)->update([
'status' => 22
]);
$result = "Registration complete.";
$html .= 'Some test code here
<br>
<p>Date: '.date('d.m.Y H:i:s').'</p>
';
$pdf = PDF::loadHTML($html);
$filename = substr(md5(uniqid().time()), 0, 17) . '.pdf';
$pdf->save(storage_path().'/app/public/uploads/rezolutii/'.$filename);
//code for download pdf HERE!!!!
return view('site.pages.registercomplete', compact('result'));
}
How can I download the pdf, after I create it?
It's impossible on Laravel.
This will not work. Your browser operates on simple requests one goes out one comes in.
There is no way for browser to know if user finished downloading the file and saved it somewhere. The final response from browser if file to be downloaded, nothing can follow that as far as I understand.
Now I'm not sure how that can be handled in javascript but in pure html requests it will not work.
Check this https://laracasts.com/discuss/channels/laravel/redirect-after-download

can't reach page while export pdf/downloading file in dompdf

I want to export data tables to PDF.
I'm using Codeigniter Framework, and use dompdf plugin.
I think the code is correct, because it doesn't show any error information when I click the button to export to PDF, the page spends a long time loading, and in the end, it just displays "can't reach the page".
This problem also happens when I try to download files from my local directory.
Here is the code:
actually issue happened when i try to download more number of records
The controller:
$this->load->library('pdf'); // change to pdf_ssl for ssl
$data = array();
$data['result'] = $finaldata;
$data['search_header'] = $search_header;
$html = $this->load->view('admin/report/school_pdf', $data, TRUE);
$this->pdf->create($html, $filename);

PHPExcel style text to center

I'm using PHPExcel with codeigniter to generate .xlx, .xlxs file, Every thing is working perfect except text formatting. Here is the screenshot of html view that I'm passing to PHPExcel library to generate .xlx file.
And Here is the output.
As you can see that text indentation and styles are removed. Here is my code used to generate output.
public function html_to_excel_download($filename, $data=null){
if ($data != null) {
// proper encoding of data UTF-8 for unicode characters
$data = chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data);
// Put the data into a temporary file
$tmpfile = time().'.html';
file_put_contents($tmpfile, $data);
// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
// Excel Writer
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
// Download File
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
unlink($tmpfile);
exit();
}
}
How can I indent title text to center?
you may try another way to do that...
Step 1) Define "$this->excel->setActiveSheetIndex(0);" at the top of function
step 2) Define style array and add it as a variable with excel object like,
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '2F4F4F')
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
)
);
Step 3) $this->excel->getActiveSheet()->getStyle('A3:Z3')->applyFromArray($styleArray);
Note: You have to use getStyle() in your code to apply specific styles on column/row
// single column A, setting e.g. horizontal alignment
$objWorksheet->getStyle('A')->getAlignment()->setHorizontal(...);
// range of columns A to K
$objWorksheet->getStyle('A:K')->getAlignment()->setHorizontal(...);
AND you can see full documentation on this site for formatting SHEET
HERE
There are two ways you can get rid of this issue, one is without json array:
$doc->getActiveSheet()->getStyle('A1:H1')->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
where $doc = new PHPExcel(); or a PHPExcel object
The second is using the json array as described here
I'm pretty confident that you'll find all instructions about this library in the official documentation

The url from an image via custom field (wordpress)

Am not even sure if this can be done but...
Ive added a feed from my forums to wordpress it works great but I need it to auto add the url of the image in a custom field from the images in the post (feed) first image would be fine as its only ahve a slider
Is there any way to do this?
Details
Ok I think I did not explain this very well so made a few screen shots
This is my slider at the minute with my
This is an imported post one other feed I was using
On this image you can see the custom field (which I have to fill in after every import)
Adding the image url into the custom field
and finaly a view of the slider working
This is what am trying to do (auto) so my feed from my booru / forums / 2 other of my sites and (2 other peoples) sites make my home page on a new site
Hope this explain it alot more
This uses the external Simple Pie library built into WordPress to fetch the feed, get the image url and create a new post for each item and save the image url as a custom field.
To activate the process we have to hook into wp_cron. The code below does it daily but it would probably be better to do it weekly to prevent overlap. Some overlap will probably occur so this still needs a way to check if we have already imported the image
First we need a function to save the custom field after the post has been created. This section comes from another answer I found on WordPress Answers.
Edit:
This needs to be wrapped in a plugin to schedule the cron event and the cron event was missing the action to make it fire.
Edit:
Final version below tested and it works but the feed the OP is getting is using relative url's so the domain name needs to be added somewhere in the output code.
<?php
/*
Plugin Name: Fetch The Feed Image
Version: 0.1
Plugin URI: http://c3mdigital.com
Description: Sample plugin code to fetch feed image from rss and save it in a post
Author: Chris Olbekson
Author URI: http://c3mdigital.com
License: Unlicense For more information, please refer to <http://unlicense.org/>
*/
//Register the cron event on plugin activation and remove it on deactivation
register_activation_hook(__FILE__, 'c3m_activation_hook');
register_deactivation_hook(__FILE__, 'c3m_deactivation_hook');
add_action( 'c3m_scheduled_event', 'create_rss_feed_image_post');
function c3m_activation_hook() {
wp_schedule_event(time(), 'weekly', 'c3m_scheduled_event');
}
function c3m_deactivation_hook() {
wp_clear_scheduled_hook('c3m_scheduled_event');
}
function create_rss_feed_image_post() {
if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://animelon.com/booru/rss/images'); // specify the source feed
}
foreach ($feed->get_items() as $item) :
// global $user_ID;
$new_post = array(
'post_title' => $item->get_title(),
'post_status' => 'published',
'post_date' => date('Y-m-d H:i:s'),
//'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
if ($enclosure = $item->get_enclosure() )
update_post_meta( $post_id, 'feed_image_url', $enclosure->get_link() );
endforeach;
}

Joomla TinyMCE editor do not save inserted image

We are building joomla component. And we use joomla editor in which we insert content.
But there is a problem, because when we add image to editor and save it, it do not add image to database and when we opening this element to edit it again, there is only text in editor, image disappears.
This is how we use it:
$editor =& JFactory::getEditor();
echo $editor->display('text', $this->hello->text, '800', '300', '20', '20');
Maybe there is need to supply aditional parameters to display method?
Problem solved.
The standard way of getting the form data $post = JRequest::get('post'); is not enough in the case of using a editor. This will filter the content, hence losing line breaks and paragraps. So we need to add an extra line to get the editor contents in a raw unfiltered way. This data will be passed to the model to save into the database.
To get HTML form post data you need to get this data in following way
$data = JRequest::getVar( 'editorName', 'defaultValue', 'post', 'string', JREQUEST_ALLOWRAW );
And need to add a javascript for the view(tmpl file)
function submitbutton(action) {
var form = document.adminForm;
switch(action)
{
case 'save':case 'apply':
<?php
$editor =& JFactory::getEditor();
echo $editor->save( 'editorName' );
?>
case 'publish':
case 'unpublish':
case 'cancel':
default:
submitform( action );
}
}

Resources