I am trying to create a PDF with my company logo using the dompdf library but I have a PDF without my logo. I can try the following code.
Welcome Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('pdf');
}
public function index()
{
$html = $this->load->view('welcome_message', [], true);
$this->pdf->createPDF($html, 'mypdf', false);
}
}
Welcome_view:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<img src="<?php echo base_url() ?>/img/un6.png'">
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
and also included dompdf files application/libraries folder:
PDF.php file for dompdf file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
class Pdf
{
function createPDF($html, $filename='', $download=TRUE, $paper='A4', $orientation='portrait'){
$dompdf = new Dompdf\DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
if($download)
$dompdf->stream($filename.'.pdf', array('Attachment' => 1));
else
$dompdf->stream($filename.'.pdf', array('Attachment' => 0));
}
}
?>
How can I do this
The problem is here:
<img src="<?php echo base_url() ?>/img/un6.png'">
DomPDF needs all external resources to be referenced as file paths, not URLs.
You could, for instance try this:
<img src="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/img/un6.png'">
or also something like:
<img src="<?php echo FCPATH; ?>/img/un6.png'">
(FCPATH is Codeigniter's front controller path... the server path to the main index.php file that orchestrates everything in CI)
DomPDF requires base64 encoded images in order to display it properly in PDF format.
Convert the image to base64 using this tool https://www.base64-image.de/
It will give a html tag, just use it in your HTML PDF code
Related
Hi i use codeigniter to simple listing script. I have a multi images upload for each listing 5 images...
But i have a problem with showing in listing details page....
i have two tables properties and media In media has 3 column id property_id (same with properties.id and photo (this is image name + extension)
Controller: Properties.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends My_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Property');
}
public function details($slug){
$this->data['listing'] = $this->Property->find_by_slug($slug);
$this->load_theme('properties/details');
}
}
Model: Property.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Property extends CI_Model{
var $table = 'properties';
function find_by_slug($slug){
$this->db->select('properties.*,users.*,media.photo');
$this->db->join('users', 'users.id = properties.user_id');
$this->db->join('media', 'media.property_id = properties.id');
$this->db->where('slug',$slug);
return $this->db->get($this->table,1)->row_array();
}
}
View: details.php
<div class="carousel-inner">
<div class="item active">
<?php $x = 0; ?>
<?php if($listing):?>
<?php foreach ($listing as $m):?>
<?php $x++ ; ?>
<div class="item <?php if ($x == 1) echo 'active' ?>">
<img src="<?php echo site_url('files/listing/'.$m['photo'])?>" class="thumb-preview" alt="<?php echo $m['alt'];?>">
</div>
<?php endforeach;?>
<?php endif;?>
</div>
I can not understand why it does not show the images in the slider...
If in config, site_url() can have index.php at the end. That will break your image url.
If you need to access your resources, always use base_url().
Change:
site_url('files/listing/'.$m['photo']);
To:
base_url('files/listing/'.$m['photo']);
I think the problem is that it can not find the appropriate id
I am using CodeIgniter bootstrap stylesheet link URL can't work properly.
Controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
$data['title'] = "Login Home";
$this->load->view('home',$data);
}
}
HTML Code
<div class="container">
<div class="row">
<div class="col-lg-offset-4 col-lg-4">
<div class="well">
<h3>welcome</h3>
</div>
</div>
</div>
</div>
CSS link code
<link href="<?php echo base_url("register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
Place assets out side of application. Reason the .htaccess in application folder blocks it.
application
assets
assets > bootstrap > css > bootstrap.css
assets > bootstrap > js > bootstrap.js
system
index.php
First I would suggest
config/autoload.php
$autoload['helper'] = array('url');
then
config/config.php Set your base url
$config['base_url'] = 'http://localhost/projectnmame/';
in config.php set this line
$config['base_url'] = 'http://localhost/register/';
root directory set this way
application
system
bootstrap-3.3.6
ensure that you are using .htaccess file in your codeigniter application.create one folder assets.place your css file in assets folder.after that link that file like this,
in application folder config file paste this,
$root = 'http://' . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);;
$config['base_url'] = $root;
<link href="<?php echo base_url("assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
if you are not using htaccess file,
<link href="<?php echo base_url("index.php/assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
try this....
I m learing codeigniter and i want to add an image in my view page.
I am using tag but the image is not added in the page.
So help me how i can add the image in view page of codigniter.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog</title>
</head>
<body>
<h2><img src="../images/logo.png"></h2>
<?php $this->load->view('blog/menu');
if ($query):foreach ($query as $post): ?>
<h4><?php echo $post->entry_name; ?> (<?php echo $post->entry_date; ?>)</h4>
<?php echo $post->entry_body; ?>
<?php endforeach;
else: ?>
<h4>No entry yet!</h4>
<?php endif; ?>
</body>
</html>
store images in an images folder at the same level as the application folder . then just link to it like this using code.
<img src="<?php echo base_url('images/logo.png'); ?>" />
Store images in an images folder at the same level as the application folder, Then just link to it like this:
<img src="../../images/image1.jpg" />
See, first you create a file with the name user_profile.php in the controller folder. no uppercase letters.
To call a View page you created in the index:
function index()
{
$this->load_controller('myView');
}
this is an example from the link i sent you that is perfect, that shows peaces of a views and how to combine them to one code from the controller.
Please go to the codeigniter website and start learning the simple stuff. or search for codeigniter via youtube. you will find a lot!
Class User_Profile extends Controller
{
function index()
{
$this->load_controller('Left_Nav');
$this->load_controller('Content_Nav');
$this->load_controller('Login_Name');
$this->load_controller('Leaderboard', 'Board');
$this->Left_Nav->index(array('highlight_selected_page' => 'blah'));
$this->load('User');
$content_data = $this->User->get_profile_details();
$this->view->load('content', $content_data);
$this->Login_Name->index();
$this->Board->index();
}
}
image in assets
<img src="<?php echo base_url(); ?>assets/images/image.png">
I have two controllers in my project. The second controller do not displays in right way. If I open it with in browser line - thats ok, but if i pass it by the link in view its not ok. The second controller resembles on 1st.
Controller(s)
<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
class Article extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
ini_set('display_errors',1);
error_reporting(E_ALL);
$this->load->view('header_view');
$this->load->view('menu_view');
$this->load->view('categories_view');
$this->load->model('mat_model');
$data = array();
$data['news'] = $this->mat_model->get_latest();
$data['latest'] = $this->mat_model->get_latest();
$this->load->view('useful_sites_view',$data);
$this->load->view('article_view',$data);
$this->load->view('footer_view');
}
}
?>
The view where I have a link:
<?php foreach ($news as $one):?>
<div id="right">
<div id="breadcrumb">Home » Somewhere</div>
<h1><?=$one['title']?></h1>
<p>
<div id="small_img"><?=$one['small_img']?></div>
<?=$one['description']?>
</p>
<div id=""> Читать далее...</div>
<span class="postinfo"> Posted by <?=$one['author']?> on
<?=$one['date']?></span>
<HR ALIGN="center" WIDTH="70%" SIZE="1px" COLOR="black">
</div>
<?php endforeach; ?>
I dont have so much reputation to browse images on the site, but i needs it so much, apologize for using otherwise resource
With line in browser:
With the link:
http://s020.radikal.ru/i710/1301/fd/76f313259454.jpg
With the browser line:
http://s020.radikal.ru/i713/1301/4e/a863cd18184d.jpg
Thanks.
In reply to the comment above...
<link rel="stylesheet" href="<?= base_url() ?>css/style.css">
is an absolute path.
Lets say that your base_url() is 'http://www.example.com/' so the above line will link to your css like so:
<link rel="stylesheet" href="http://www.example.com/css/style.css">
so no matter which page you're on, the CSS will always be linked to that css file.
On the other hand if you were using relative paths like this:
<link rel="stylesheet" href="/css/style.css">
on your site root, the css path would be correct. BUT on some other page, for example
http://www.example.com/somepage/testurl
your CSS would be linked like this:
<link rel="stylesheet" href="http://www.example.com/somepage/testurl/css/style.css">
which is obviously, wrong.
Try to google relative and absolute paths in html, that should make things a lot clearer.
Hey i am totally new in Codeigniter. In the Controllers folder I created a file named caller.php and I created a file home1.php in \Views. In root directory i created an image folder named \Images and also created a css folder named \css. In images Folder there are 6 picture. In css folder style.css file exist.
In caller.php i write
<?
class caller extends CI_Controller
{
function index()
{
$this->load->view('home1');
// what i have to write here lo load images....
}
}
In home1.php i write
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
// what i have to write here to load images
</head>
<body>
<div id="outer">
<div id="container">
<div id="images">
<img src="new.jpg" width="960" height="400"/>
<img id="image1" src="1.jpg" />
<img id="image2" src="2.jpg" />
<img id="image3" src="3.jpg" />
<img id="image4" src="4.jpg" />
<img id="image5" src="5.jpg" />
</div>
<div id="slider">
1
2
3
4
5
</div>
...................................................
....................................................
</div>
</div>
</body>
</html>
Now for the above code how i load the images .Please experts help me.
if additional config's are needed please mention that.
As you're already using the url helper I suggest wrapping your image src attributes with base_url, such as:
<img src="<?php echo base_url('images/1.jpg'); ?>" />
And as mentioned in the other answer it's best (mandatory?) to capitalize the class name in your controller
class Caller extends CI_Controller { ...
First of all, you need to capitalize your class name.
class Caller extends CI_Controller {
public function index()
{
// method code goes here
}
}
Then, you need to link to those images with absolute links or with the CI URL helper: "/images/1.jpg" and so on.
How to use with the CI URL helper is detailed here : Helper
EDIT
Load the URL helper with this in your constructor method:
$this->load->helper('url');
You can create a URL like this:
echo base_url("blog/post/123");
That will make:
http://example.com/index.php/news/local/123
Or
http://example.com/news/local/123
If you've taken out the index.php in your config file.
Here's a class with the constructor that calls the URL helper:
class Caller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
// method code goes here
}
}
Make folder in root directory and code easily in views html file.
if directory name is directoryName and image name imageName.jpg
you can call like this.
<img src="directoryName/imageName.jpg">
You can do the same for as you did to Load the CSS files.
Create a folder in Root Directory. Create a sub folder(if you have Multisite)
then point your base_url in config file to your base path.
and then do the same
<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />
Only simple way
<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />
That sit!!!