PHP Wkhtmltopdf not found - php

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.

Related

How to output an image to pdf in table specific php code?

I am being asked to only add code to the following function. I want to output a logo and additional text if possible. And the content of the export document is $this->ExportDoc->Text. I am using the following but it's not working.
The original example is
function Page_Exporting() {
$this->ExportDoc->Text = "my header";
return TRUE;
}
I tried the following and they wouldn't work
function Page_Exporting() {
$this->ExportDoc->Text('logo.jpg');
return TRUE;
}
or
I tried to add additional lines, the second always replace the previous, they refuse to show at the same time....
function Page_Exporting() {
$this->ExportDoc->Text = "my header1";
$this->ExportDoc->Text = "my header2";
return TRUE;
}
I just want to add logo.jpg and couple lines to the top of the pdf I want to export from php code.
With this lib it seems easier !
https://github.com/PHPOffice/PHPWord
Example of the official documentation :
https://phpword.readthedocs.io/en/latest/elements.html#images
PHPWord can generate pdf !
In other hand, are you sure about your path ?
$this->ExportDoc->Text('logo.jpg');
logo.jpg is on the same path as your php script ?
For example to get the current path of your script use __DIR__.
https://secure.php.net/manual/en/language.constants.predefined.php
And finaly had the absolute path use realpath.
https://www.php.net/manual/en/function.realpath.php
Example of code :
$path = realpath(__DIR__ . '/../Resources/Asset/images/logo.png');
ExportDoc isn't a php native capability, so what are you using ?

Starting external programme from PHP

I am running in a Windows10 environment. My php script writes a pdf file and I simply want to have the file open up in the pdf viewer that I specify. I am using the "runAsynchronously" function given in the PHP manual and I have tried many variations. I have no problem getting the process to run in the background - it appears every time in my TaskManager process listing, but no window appears - what am I doing wrong? If I double click the link file that has been written it works fine. It is nothing to do with the path to the executable or the filename - I can replace the pdf viewer with "notepad.exe" and the $file with a suitable text file - the same thing happens, notepad appears as a process, but not as a window, and the link works fine.
Here are some code snippets
$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
runAsynchronously($cmd, $file, 7, null, true);
function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) {
$tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile;
try {
if(file_exists($tmp)) { unlink($tmp); }
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut($tmp);
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$waitforcompletion = false;
if($exec) {
// Run kicks off the process in the background, but no window gets opened
$oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion);
unlink($tmp);
} // if not executed link is left available for manual running
unset($WshShell,$oShellLink,$oExec);
} catch(Exception $ex) {
print $ex->getMessage();
}
}
If opening in the browser a pdf or downloading it solves your problem, try a class called PDFMerger:
https://github.com/clegginabox/pdf-merger
It uses an API called setasign FPDI and FPDF (https://packagist.org/packages/setasign/fpdi-fpdf#p-456)
I tested it here, because it also interested me and worked ok. (Wamp in Windows 8.1)
I put everything together(classes and folders of fpdi and fpdf) in a folder called pdfmerger because I was having some difficulty with namespaces and my code worked ok, but I know this is not the best way to include the classes - the example in GitHub page must help if you have problems with classes not finding classes):
<?php
include "pdfmerger/fpdf.php";
include "pdfmerger/fpdi.php";
include 'pdfmerger/PDFMerger.php';
$pdf = new PDFMerger();
//generate a pdf with the pages 1,2 and 3 and send to browser
$pdf->addPDF('originalpdfsavedintheserver.pdf', '1,2,3')
->merge('browser', 'nameinbrowser.pdf');
// REPLACE 'browser' WITH 'file', 'download', 'string', or 'browser' for output options
If you use the option 'download' you can choose the program that will open the pdf file in your windows settings(Choose default program to open pdfs):
https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/

Class 'Smalot\PdfParser\Parser' not found

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();

MPDF not generating anything on external server

I've generated a beautiful HTML invoice with MPDF, but when I placed it on the server it isn't showing anything.
The logs show the following for local (here the generation works):
http://pastebin.com/n3xJujBH
The logs show the following when generating on the server (here it shows an empty HTML page on generation, not PDF):
http://pastebin.com/HDeSPHse
The following code is used to generate the PDF in Codeigniter:
private function _gen_pdf($html,$paper='A4')
{
$this->load->library('mpdf53/mpdf');
$mpdf=new mPDF('utf-8',$paper);
$mpdf->debug = true;
$mpdf->WriteHTML($html);
$mpdf->Output();
}
The HTML created is following: http://pastebin.com/b3hFNbT8
Something to note is that, if I put only "test" in $html, it won't generate either.
Any ideas?
This got me pass the obstacle
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'tempDir' => __DIR__ . '/custom/tmp']);
Create custom (this can be anything) directory if it tells some permission error on the live server like on aws ubuntu instance etc...
The answer for me was switching to DomPDF. I never got in functioning on the external parallels server and will probably never know why..
I was facing the same issue.
I tried this code and it gave me the issue due to which mpdf was not working.
<?php
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$mpdf->WriteHTML("Hello World");
$mpdf->Output();
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception
// name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
?>
In my case i had to make the folder where i had kept my files writable.

Doc to PDF with PHP + Openoffice

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.

Categories