Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts

Friday, May 18, 2012

How to change "font-family" in dompdf script

In latest web development industry document conversion from one format to other format is very important. Sometimes we need to generate spread sheet files, some times we need to generate word files and some times PDF files. In this topic I will be talking about PDF file conversion.

For generating PDF files from the given data, there are a number of scripts and modules written in php and different languages. DomPDF is one of them written in php. DomPDF is fabulous in its features. It allows direct conversion of HTML files to PDF files. But there are some limitations like some times it never generates the exact copy of that HTML. Some times font-family doesn't apply correctly as we apply in HTML file. I had the same problem last days when I was using DomPDF for my PDF generation. I goggled the web and found some solutions. And on the basis of those many solutions and posts, I wrote a summarized article focusing on this problem.

It need some simple steps to follow,

  1. Download archive from approved answer. Extract files /dompdf/lib/fonts/times*.*.
  2. Copy them in your dompdf fonts folder.
  3. Edit dompdf_font_family_cache.dist.php with snippet 1.
  4. In CSS use font-family: times;.

Snippet 1:

 /* ... */  
 'times' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 'times-roman' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 /* ... */  

If you want to use your own TTF font (say, Arial.ttf):
  1. Run: ttf2afm -o Arial.afm Arial.ttf. (I did it in Ubuntu.)
  2. Run: ttf2ufm -a -F Arial.ttf. (you can use /dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.)
  3. Copy Arial.* files in /dompdf/lib/fonts/.
  4. Add to dompdf_font_family_cache.dist.php snippet 2.
  5. In CSS use font-family: arial;.

Snippet 2:

 /* ... */  
 'arial' => array (  
   'normal' => DOMPDF_FONT_DIR . 'Arial',  
   'bold' => DOMPDF_FONT_DIR . 'Arial',  
   'italic' => DOMPDF_FONT_DIR . 'Arial',  
   'bold_italic' => DOMPDF_FONT_DIR . 'Arial'  
 )  
 /* ... */  

 Please feel free to post you comments and suggestions.

Thanks
 

Monday, April 30, 2012

Downloading a file forcibly using php


This is a very important and useful topic and we programmers normally need this kind of script in our websites etc. I wrote this scripts and now I am going to share you this to my blog readers.

The idea behind this is to use php header parameters to do this. Whenever you try to open some file which browser cant open (files like .doc, .docx etc ) itself, browser prompts you a download window and will ask you to download the file. This thing is done automatically by browser for some file formats. But what will happen when you open a .html, or .php file using a url? The answer to this is that browser will open or try to run these files in the browser window. But if you want to just download the files and not to open them in browser, then you have to tell php that download these files. This can be done by using some php header settings. The class that I am using for this thing is shown below.

 <?php  
 session_start();  
 $path="";  
 $filename = $path.$_GET['file'];  
 // required for IE, otherwise Content-disposition is ignored  
 if(ini_get('zlib.output_compression'))  
  ini_set('zlib.output_compression', 'Off');  
 // addition by Jorg Weske  
 $file_extension = strtolower(substr(strrchr($filename,"."),1));  
 if( $filename == "" )   
 {  
  echo "<html><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";  
  exit;  
 } elseif ( ! file_exists( $filename ) )   
 {  
  echo "<html><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";  
  exit;  
 };  
 switch( $file_extension )  
 {  
  case "pdf": $ctype="application/pdf"; break;  
  case "exe": $ctype="application/octet-stream"; break;  
  case "zip": $ctype="application/zip"; break;  
  case "doc": $ctype="application/msword"; break;  
  case "docx": $ctype="application/msword"; break;  
  case "xls": $ctype="application/vnd.ms-excel"; break;  
  case "xlsx": $ctype="application/vnd.ms-excel"; break;  
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;  
  case "pptx": $ctype="application/vnd.ms-powerpoint"; break;  
  case "gif": $ctype="image/gif"; break;  
  case "png": $ctype="image/png"; break;  
  case "jpeg":  
  case "jpg": $ctype="image/jpg"; break;  
  default: $ctype="application/force-download";  
 }  
 header("Pragma: public"); // required  
 header("Expires: 0");  
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
 header("Cache-Control: private",false); // required for certain browsers   
 header("Content-Type: $ctype");  
 // change, added quotes to allow spaces in filenames, by Rajkumar Singh  
 header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );  
 header("Content-Transfer-Encoding: binary");  
 header("Content-Length: ".filesize($filename));  
 readfile("$filename");  
 exit();  
 ?>  

you can download this script using this url
http://dl.dropbox.com/u/37908711/public_scripts/php_downloadfile.rar

How to resolve 1045 error when installing xamp

Some times whenever we install xamp on our system we get 1045 error in phpmyadmin. Here is the screen shot of that error,


I have found the solution of this error. Just un-comment // $cfg['Servers'][$i]['AllowNoPassword'] = TRUE; in phpmyadmin/config.inc.php file. Enjoyee....