I am using PDFTOHTML (a php library) to convert pdf files to html and it's working fine but it's showing converted file in a browser and not storing in local folder, i want to store converted html in local folder using php with the same name as pdf was i-e mydata.pdf to mydata.html
Code that is converting pdf to html is:-
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', [
'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);
// get content from all pages and loop for they
foreach ($pdf->getHtml()->getAllPages() as $page) {
echo $page . '<br/>';
}
?>
Just change your foreach to
$filePdf = 'cv'; // your pdf filename without extension
$pdf = new \TonchikTm\PdfToHtml\Pdf($filePdf.'.pdf', [
'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);
$counterPage = 1;
foreach ($pdf->getHtml()->getAllPages() as $page) {
$filename = $filePdf . "_" . $counterPage.'.html'; // set as string directory and filename where you want to save it
if (file_exists($filename)) {
// if file exist do something
} else {
// else
$fileOpen = fopen($filename, 'w+');
fputs($fileOpen, $page);
fclose($fileOpen);
}
$counterPage++;
echo $page . '<br/>';
}
This will create you file for example: example_1.html, example_2.html and so on.
if this not help you then probably you need to use file_put_contents with ob_start() and ob_get_contents() read more here
Look this :
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', ['pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe', 'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe']);
// get content from all pages and loop for they
$file = fopen('cv.html', 'w+');
$data = null;
foreach ($pdf->getHtml()->getAllPages() as $page) {
$data .= "".$page."<br/>";
}
fputs($file, $data);
fclose($file);
I did not test this code
I've been trying to print a PDF using mPDF and codeigniter. This PDF as well as all the other ones in the application print fine, but I can't get it to print landscape.
I've tried all the combinations of parameters as suggested in other questions, but none of them has had any effect on the printing. Therefore it has to be something wrong with the way I'm implementing the mPDF library.
My current controller:
$filename = $order_number;
$pdfFilePath = FCPATH."/downloads/orders/$filename.pdf";
$data['page_title'] = 'Order';
if (file_exists($pdfFilePath) == TRUE)
{
unlink($pdfFilePath);
}
if (file_exists($pdfFilePath) == FALSE)
{
ini_set('memory_limit','128M'); // boost the memory limit
$html = $this->load->view('generate_pdf', $data, true); // render the view into HTML
$this->load->library('pdf2');
$pdf2 = $this->pdf2->load();
$pdf2->WriteHTML($html); // write the HTML into the PDF
$pdf2->Output($pdfFilePath, 'F'); // save to file because we can
}
The mPDF library:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pdf2 {
function pdf2()
{
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param=NULL)
{
include_once APPPATH.'/third_party/mpdf/mpdf.php';
if ($params == NULL)
{
$param = '"en-GB-x","A4-L","","",10,10,10,10,6,3';
//$param = '"en-GB-x","A4","","",10,10,10,10,6,3',L;
//$param = '"en-GB","A4-L"';
//$param = '"L"';
//$param = '\'en-GB-x\',\'A4-L\',\'\',\'\',10,10,10,10,6,3,L';
}
//echo $param;
return new mPDF($param);
}
}
Is mPDF ignoring the parameters or do I have it wrong?
I have been trying to debug this code for a while now, and it looks like the build method is not being called. I put echo's, var_dumps, and all other kinds of signs in it, but never get anything.
Full php code
class Auto_slideshow {
private $_img_dir;
//constructor sets directory containing the images
function __construct($dir) {
//Directory source from webroot
$_img_dir = $dir;
}
//Iterates through directory and constructs HTML img list
public function build() {
//use SPL for directory iteration
$iterator = new DirectoryIterator($_img_dir);
$html = '';
$i = 1;
//Iterate through directory
foreach ($iterator as $file) {
//set a variable to add class of "show" on first element
$showClass = $i === 1 ? ' class="show"' : null;
//exclude '.' and '..' files
if(!$iterator->isDot()) {
//set src attribute for img tag
$src = $_img_dir . $iterator->getFilename();
//Grab sizes of images
$size = getimagesize($_img_dir . $iterator->getFilename());
var_dump($src);
//create img tags
$html .= '<img src="' . $src . '" ' . $size[3] . $displayNone . ' />' . "\r\n";
$i++;
}
}
return $html;
}
}
html call
<center>
<div class="imagecontainer" id="auto-slideshow">
<?php
$show = new Auto_slideshow("../CMSC/images/master/slidestock/");
$show->build();
?>
</div>
</center>
I also tried print $show->build();, as the tutorial showed, which also did not work.
Update
I changed $_img_dir to $this->$_img_dir' and called the method byecho show->build();` and the method still isn't being called.
This is a matter of the method not even running, not even to the point of find the images yet.
Update 2
If I remove the entire php code within the html, the rest of the page loads just fine. As it is now, the html loads only to the div that contains the php then stop everything after it.
Have you used a var_dump() outside of the loop as well?
The problem is that your variable will remain NULL:
//constructor sets directory containing the images
function __construct($dir) {
//Directory source from webroot
// This is a local variable that will be gone when the constructor finishes:
$_img_dir = $dir;
}
You need:
//constructor sets directory containing the images
function __construct($dir) {
//Directory source from webroot
$this->_img_dir = $dir;
^^^^^^ here
}
You return the text you want to display but you don't display it:
echo $show->build();
Fix and update the code bellow:
Constructor:
$this->_img_dir = $dir;
build Function:
$iterator = new DirectoryIterator($this->_img_dir);
$src = $this->_img_dir . $iterator->getFilename();
$size = getimagesize($this->_img_dir . $iterator->getFilename());
You can call it:
echo $show->build();
Are there any good implementations of Minify integration with Zend Framework? I'm looking for examples.
I'd love to have a plugin that overrides $this->headLink() and spits out the correct minified url/content.
Edit:
It seems most examples I find aren't fully optimized in one form or fashion. I'm looking for a solution that meets the following requirements:
Reduces multiple links and script tags to one request (one for link and one for scripts)
The closest I've seen is a request path that passes a comma-delimited string to /min/ like so:
<script src="/min?f=file1.js,file2,js,file3.js" type="text/javascript"></script>
Why not something that combines all scripts into one file on disk on the fly and then caches it so that you aren't doing the minification on every request?
<script src="/js/app.js?someRandomStringHere" type="text/javascript"></script>
The combining aspect should maintain order (in reference to prepend, append, etc)
While I don't care so much about sending correct expires headers because I force gzipping, etags, and expires headers on the server-side, having that optional would be beneficial to other users.
Lastly, having a build script that generates the minified assets isn't necessary bad - as long as it is easy to do and doesn't require a code change after every build.
This is what I use, the class is shown followed by use cases. I've commented it quickly, some of it might need changing to match your paths or define() the PUBLICPATH
class View_Helper_Minify extends Zend_View_Helper_Abstract
{
public function minify($files, $ext, $folderName)
{
// The folder of the files your about to minify
// PUBLICPATH should be the path to your public ZF folder
$folder = PUBLICPATH . $folderName . "/";
// Set update needed flag to false
$update_needed = false;
// This is the file ext of the cached files
$cacheFileExt = "." . $ext;
// The list of files sent is exploded into an array
$filesExploded = explode(',', $files);
// The full cached file path is an md5 of the files string
$cacheFilePath = $folder . md5($files) . $cacheFileExt;
// The filename of the cached file
$cacheFileName = preg_replace("#[^a-zA-Z0-9\.]#", "", end(explode("/", $cacheFilePath)));
// Obtains the modified time of the cache file
$cacheFileDate = is_file($cacheFilePath) ? filemtime($cacheFilePath) : 0;
// Create new array for storing the list of valid files
$fileList = array();
// For each file
foreach($filesExploded as $f)
{
// determine full path of the full and append extension
$f = $folder . $f . '.' . $ext;
// If the determined path is a file
if(is_file($f))
{
// If the file's modified time is after the cached file's modified time
// Then an update of the cached file is needed
if(filemtime($f) > $cacheFileDate)
$update_needed = true;
// File is valid add to list
$fileList[] = $f;
}
}
// If the cache folder's modified time is after the cached file's modified time
// Then an update is needed
if(filemtime($folder) > $cacheFileDate)
$update_needed = true;
// If an update is needed then optmise the valid files
if($update_needed)
$this->optmiseFiles($fileList, $cacheFilePath, $ext);
// Finally check if the cached file path is valid and return the absolute URL
// for te cached file
if(is_file($cacheFilePath))
return "/" . $folderName . "/" . $cacheFileName;
// Throw Exception
throw new Exception("No minified file cached");
}
private function optimise($code, $ext)
{
// Do not optmise JS files
// I had problems getting JS files optmised and still function
if($ext == "js")
return $code;
// Remove comments from CSS
while(($i = strpos($code, '/*')) !== false)
{
$i2 = strpos($code, '*/',$i);
if($i2 === false)
break;
$code = substr($code, 0, $i).substr($code, $i2 + 2);
}
// Remove other elements from CSS
$code = str_replace('/*','',$code);
$code = str_replace("\n",' ',$code);
$code = str_replace("\r",' ',$code);
$code = str_replace("\t",' ',$code);
$code = #ereg_replace('[ ]+',' ',$code);
$code = str_replace(': ',':', $code);
$code = str_replace('; ',';', $code);
$code = str_replace(', ',',', $code);
$code = str_replace(' :',':', $code);
$code = str_replace(' ;',';', $code);
$code = str_replace(' ,',',', $code);
// Return optimised code
return $code;
}
// Optmise the list of files
private function optmiseFiles($fileList, $cacheFilePath, $ext)
{
// Empty String to start with
$code = '';
// Check files list in case just one file was passed
if(is_array($fileList))
{
// Foreach of the valid files optmise the code if the file is valid
foreach($fileList as $f)
$code .= is_file($f) ? $this->optimise(implode('', file($f)), $ext) : '';
}
// Else if a valid file is passed optmise the code
else
$code = is_file($fileList) ? $this->optimise(implode('', file($fileList)), $ext) : '';
// Open the cache file
$f = #fopen($cacheFilePath, 'w');
// If open successful
if(is_resource($f))
{
// Write code to the cache file
fwrite($f, $code);
// close cache file
fclose($f);
}
}
}
You would use the helper like this in your view
// Define an array of files, note you do not define the ext of those files
// The ext is defined as a param for the helper as this effects the optmisation
$files = array("jquery-ui-1.8.7.custom",
"jquery.pnotify.default",
"jquery.pnotify.default.icons",
"tipTip",
"prettyPhoto",
"custom");
// Get the absolute URL of the files which are imploded also pass the directory 'css' and ext 'css'
$cssString = $this->minify(implode("," , $files), "css", "css");
// use baseURL() to output the full URL of the cached file and use it as normal with headLink()
echo $this->headLink()
->appendStylesheet($this->baseUrl($cssString));
And here is a javascript version
$files = array("jquery-1.4.4.min",
"jquery.pnotify.min",
"jquery.tipTip.minified",
"jquery.countdown.min",
"jquery.prettyPhoto",
"jquery.typewatch",
"default.functions");
$jsString = $this->minify(implode("," , $files), "js", "scripts");
echo $this->headScript()->appendFile($this->baseUrl($jsString));
I am trying to do the same thing right now. I am looking at NC State University's OT Framework, based on Zend Framework. This is implemented as a view helper. It has a nice class to minify all headscripts and headlinks via the Minify on Google Code:
http://ot.ncsu.edu/2010/03/03/getting-started-with-ot-framework/
Headscripts:
<?php
/**
* Minifies the javascript files added via the minifyHeadScript helper using
* minify (http://code.google.com/p/minify/)
*
*/
class Ot_View_Helper_MinifyHeadScript extends Zend_View_Helper_HeadScript
{
protected $_regKey = 'Ot_View_Helper_MinifyHeadScript';
public function minifyHeadScript($mode = Zend_View_Helper_HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
{
return parent::headScript($mode, $spec, $placement, $attrs, $type);
}
public function toString()
{
$items = array();
$scripts = array();
$baseUrl = $this->getBaseUrl();
// we can only support files
foreach ($this as $item) {
if (isset($item->attributes['src']) && !empty($item->attributes['src'])) {
$scripts[] = str_replace($baseUrl, '', $item->attributes['src']);
}
}
//remove the slash at the beginning if there is one
if (substr($baseUrl, 0, 1) == '/') {
$baseUrl = substr($baseUrl, 1);
}
$item = new stdClass();
$item->type = 'text/javascript';
$item->attributes['src'] = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $scripts);
$scriptTag = $this->itemToString($item, '', '', '');
return $scriptTag;
}
public function getMinUrl() {
return $this->getBaseUrl() . '/min/';
}
public function getBaseUrl(){
return Zend_Controller_Front::getInstance()->getBaseUrl();
}
}
And here is the code for headlinks:
<?php
/**
* Minifies the stylesheets added via the minifyHeadLink helper using
* minify (http://code.google.com/p/minify/)
*
*/
class Ot_View_Helper_MinifyHeadLink extends Zend_View_Helper_HeadLink
{
protected $_regKey = 'Ot_View_Helper_MinifyHeadLink';
public function minifyHeadLink(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
{
return parent::headlink($attributes, $placement);
}
public function toString()
{
$items = array();
$stylesheets = array();
$baseUrl = $this->getBaseUrl();
foreach ($this as $item) {
if ($item->type == 'text/css' && $item->conditionalStylesheet === false) {
$stylesheets[$item->media][] = str_replace($baseUrl, '', $item->href);
} else {
$items[] = $this->itemToString($item);
}
}
//remove the slash at the beginning if there is one
if (substr($baseUrl, 0, 1) == '/') {
$baseUrl = substr($baseUrl, 1);
}
foreach ($stylesheets as $media=>$styles) {
$item = new stdClass();
$item->rel = 'stylesheet';
$item->type = 'text/css';
$item->href = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $styles);
$item->media = $media;
$item->conditionalStylesheet = false;
$items[] = $this->itemToString($item);
}
$link = implode($this->_escape($this->getSeparator()), $items);
return $link;
}
public function getMinUrl() {
return $this->getBaseUrl() . '/min/';
}
public function getBaseUrl(){
return Zend_Controller_Front::getInstance()->getBaseUrl();
}
}
I ran across the same problem and ended up writing two drop-in helpers to manage it for me. You can see them at http://blog.hines57.com/2011/03/13/zendframework-minify/ - thanks again. Quick overview for one of them:
* * ** PREREQUISITES **
* This file expects that you have installed minify in ../ZendFramworkProject/Public/min
* and that it is working. If your location has changed, modify
* $this->$_minifyLocation to your current location.
*
* ** INSTALLATION **
* Simply drop this file into your ../ZendFramworkProject/application/views/helpers
* directory.
*
* ** USAGE **
* In your Layout or View scripts, you can simply call minifyHeadLink
* in the same way that you used to call headLink. Here is an example:
*
echo $this->minifyHeadLink('/favicon.ico') // Whatever was already loaded from Controller.
->prependStylesheet('http://example.com/js/sample.css')// 6th
->prependStylesheet('/js/jqModal.css') // 5th
->prependStylesheet('/js/jquery.alerts.css') // 4th
->prependStylesheet('/templates/main.css') // 3rd
->prependStylesheet('/css/project.css.php') // 2nd
->prependStylesheet('/css/jquery.autocomplete.css') // 1st
->appendStylesheet('/css/ie6.css','screen','lt IE 7'); // APPEND to make it Last
*
*
* This can be interesting because you will notice that 2nd is a php file, and we
* have a reference to a favicon link in there as well as a reference to a css file on
* another website. Because minify can't do anything with that php file (runtime configured
* css file) nor with CSS on other websites, and order is important,you would notice that
* the output in your browser will looks something like:
*
<link href="/min/?f=/css/jquery.autocomplete.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/css/project.css.php" media="screen" rel="stylesheet" type="text/css" />
<link href="/min/?f=/templates/main.css,/js/jquery.alerts.css,/js/jqModal.css" media="screen"
rel="stylesheet" type="text/css" />
<link href="http://example.com/js/sample.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/favicon.ico" rel="shortcut icon" />
<!--[if lt IE 7]> <link href="/css/ie6.css" media="screen" rel="stylesheet" type="text/css" /><![endif]-->
mmm , sorry i don't have examples but i can help you explaining how ,
my simple workflow would be like this :
1- as a view helper
in your custom library folder , create a class that extends the static function of my_minify::minify()
you may create a viewhelper that override the functionality of both headLink() and minfy class
2- as a plugin :
you might create plugin that runs postdispatch to minify the whole resulting view , more explanation