I am trying to use Pdfparser library to parse a PDF file but I have some issues with classes inclusion.
I read the documentation but it doesn't works.
I use Windows and XAMPP.
I created a directory in /xampp/htdocs/pdf_import
I installed Composer and I've generated the /vendor/autoload.php in pdfparser-master/src
I use the code example in documentation
Example:
<?php
require 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
// Retrieve all pages from the pdf file.
$pages = $pdf->getPages();
// Loop over each page to extract text.
foreach ($pages as $page) {
echo $page->getText();
}
When I run the php script I obtain this error:
Fatal error: Class 'Smalot\PdfParser\Parser' not found in C:\xampp\htdocs\pdf_import\pdfparser-master\src\import.php on line 8
Somehow your path is not good for
require 'vendor/autoload.php';
Verify if autoload is actually included.
In Codeigniter3/4 be sure you put the path in your config file
$config['composer_autoload'] = 'vendor/autoload.php';
then in your controller/library
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile(FCPATH . 'includes/temp/' . $pdf_file);
return $pdf->getText();
Related
I am trying to password protect PDF documents with a PHP script. So I downloaded and included the FPDI_Protection.php file in my script which contains the FPDI_Protection class.
But when I'm trying to execute the script it says the Class FPDI_Protection not found when its clearly present in the mentioned file. I also checked other answers on stackoverflow which says to use setasign\ but no such namespace is declared in file containing the class.
Also I'm not using composer for dependencies.
My Code goes like
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function pdfEncrypt ($origFile, $password, $destFile){
require_once("assets/fpdi/FPDI_Protection.php");
$pdf =& new FPDI_Protection();
// set the format of the destinaton file
$pdf->FPDF("P", "in", array('8.27','11.69'));
//calculate the number of pages from the original document
$pagecount = $pdf->setSourceFile($origFile);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++) {
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
//password for the pdf file
$password = "test";
//name of the original file
$origFile = "cv.pdf";
//name of the destination file
$destFile ="cv_test.pdf";
//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );
?>
I have my FPDI_Protection.php file under assets folder of current directory of the script.
And I have downloaded the script from here.
In case that link doesn't work see the whole article here.
Tried all possible way to fix but none of them work. Can you please point out the mistake I am doing ?
You should not use a script from a blog post which is nearly 14 years old! Just use the official and up to date version of the FPDI Protection class which is available here.
I want to convert a webpage to PDF in PHP using the library wkhtmltopdf. I'm using this PHP wrapper mikehaertl/phpwkhtmltopdf available here. I downloaded wkhtmltopdf binary from here and I used the below PHP code to point to it :
require __DIR__.'/vendor/autoload.php';
$pdfName = 'Invoice1.pdf';
$pdf = new mikehaertl\wkhtmlto\Pdf("some HTML code here");
$pdf->binary = __DIR__.'/wkhtmltopdf_0.12.4/bin/wkhtmltopdf';
if (!$pdf->send($pdfName, true)) {
echo $pdf->getError();
}
I'm getting this error: sh: 1: /var/www/mysite/public_html/wkhtmltopdf_0.12.4/bin/wkhtmltopdf: not found
I don't understand why it is saying file not found. I tried to check with PHP if the file exists using file_exists and it returns true when I provide to it that location.
Any help please?
Thanks.
I have downloaded the updated version of mpdf and using it with php. It is giving me the following error.
"fatal error: Trait 'Mpdf\Strict' not found in
E:\xampp\htdocs\PDF\mpdf\Mpdf.php on line 39".
$html = '<h2>mpdf test.</h2>';
include("mpdf/mpdf.php");
$mpdf = new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first
level of a list
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->WriteHTML($html,2);
$mpdf->Output('mpdf.pdf','I');
exit;
You are directly including mpdf via
include("mpdf/mpdf.php");
which will only include the core file but not any other file mPDF may need in the generation process. The correct way would be to use:
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf();
This will make sure that required classes will be autoloaded as soon as they are referenced.
For more info, check the "Getting started" chapter on the mPDF web page.
I am trying to follow a tutorial on converting doc to pdf using openoffice. I have the following code:
<?php
set_time_limit(0);
function MakePropertyValue($name, $value,$osm){
$oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
}
function word2pdf($doc_url, $output_url){
// Invoke the OpenOffice.org service manager
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
// Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
// Launch the desktop
$top = $osm->createInstance("com.sun.star.frame.Desktop");
// Load the .doc file, and pass in the "Hidden" property from above
$oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args);
// Set up the arguments for the PDF output
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
// Write out the PDF
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
}
$output_dir = './';
$doc_file = './test.docx';
$pdf_file = 'DpmR5Reqv1.20.pdf';
$output_file = $output_dir . $pdf_file;
$doc_file = 'file:///' . $doc_file;
$output_file = 'file:///' . $output_file;
word2pdf($doc_file,$output_file);
?>
I get the error:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `com.sun.star.ServiceManager' in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
( ! ) com_exception: Failed to create COM object `com.sun.star.ServiceManager': Invalid syntax in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
Ive tried to what this tutorial suggests: http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/ But no luck. Any idea what I can do? I am running this under wamp and it will be ran under wamp in production.
You have to have OpenOffice setup to run as a service on that machine. To simply convert an odt to a pdf you can use pyodconverter. They also explain how to setup the local OpenOffice service:
http://www.artofsolving.com/opensource/pyodconverter
I'm using this technique in a script I wrote and have an article for here:
http://codeuniversity.com/scripts/scr1
Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.
it is much easier to use headless libreoffice and a php wrapper class like https://github.com/ncjoes/office-converter.
of course you have to install libreoffice and you must have full control of your webserver.
So I have a client who's current host does not allow me to use tar via exec()/passthru()/ect and I need to backup the site periodicly and programmaticly so is there a solution?
This is a linux server.
PHP 5.3 offers a much easier way to solve this issue.
Look here: http://www.php.net/manual/en/phardata.buildfromdirectory.php
<?php
$phar = new PharData('project.tar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
?>
At http://pear.php.net/package/Archive_Tar you can donload the PEAR tar package and use it like this to create the archive:
<?php
require 'Archive/Tar.php';
$obj = new Archive_Tar('archive.tar');
$path = '/path/to/folder/';
$handle=opendir($path);
$files = array();
while(false!==($file = readdir($handle)))
{
$files[] = $path . $file;
}
if ($obj->create($files))
{
//Sucess
}
else
{
//Fail
}
?>
There is the Archive_Tar library. If that can't be used for some reason, the zip extension might be another option.
I need a solution that would work on Azure websites (IIS) and had trouble with creating new files on the server using methods from other answers. The solution that worked for me was to use small TbsZip library for compression, which doesn't require to write file anywhere in the server - it's just returned directly via HTTP.
This thread is old, but this approach might be a bit more generic and complete answer, so I post the code as alternative:
// Compress all files in current directory and return via HTTP as a ZIP file
// by buli, 2013 (http://buli.waw.pl)
// requires TbsZip library from http://www.tinybutstrong.com
include_once('tbszip.php'); // load the TbsZip library
$zip = new clsTbsZip(); // instantiate the class
$zip->CreateNew(); // create a virtual new zip archive
// iterate through files, skipping directories
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($objects as $name => $object)
{
$n = str_replace("/", "\\", substr($name, 2)); // path format
$zip->FileAdd($n, $n, TBSZIP_FILE); // add fileto zip archive
}
$archiveName = "backup_".date('m-d-Y H:i:s').".zip"; // name of the returned file
$zip->Flush(TBSZIP_DOWNLOAD, $archiveName); // flush the result as an HTTP download
And here's the whole article on my blog.