I am converting docx file to pdf using PHP library PHPOffice and PHPWord. I am using TCPDF as PDF writer.
My code is as below
include_once 'Sample_Header.php';
include_once '../vendor/tecnickcom/tcpdf/tcpdf.php';
\PhpOffice\PhpWord\Settings::setPdfRendererPath('../vendor/tecnickcom/tcpdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$temp = \PhpOffice\PhpWord\IOFactory::load('files/sampledocument.docx');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($temp , 'PDF');
$xmlWriter->save('results/sampledocument.pdf', TRUE);
Its working fine and generating correct pdf file only if docx file is containing plain text without any style changes(i.e color, text-bold). But if docx file containing stylings then in pdf its starting from new line.
For example docx file contains
Hello World
This is showing correct in pdf file. But if docx file contains as below ("H" and "W" is bold here)
**H**ello **W**orld
Its showing in pdf as below (Instead of showing in one line its showing in multiple lines)
H
ello
W
orld
Please let me know you any one is having solution for this. Thanks in advance.
I have a task of reading pdf files after an upload in the DB or n a folder,
What is the question here is : How to read PDF files in PHP or JS, JQuery, AJAX,
Then i want to recuperate the datas to inject in a form fields.
There's a lot of infos to do this process with text files but pdf seems complicated. There is a PHP class for that ? I'm not used to classes in Php but with infos, it would lead me.
Thanks a lot for help!!
Have a grreat one!
I managed to do this using http://www.pdfparser.org/
I needed the specifications from a pdf file and get all the raw text. This is the code I used:
<?php
include 'pdfparser-master/vendor/autoload.php';
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('specs.pdf');
$text = $pdf->getText();
echo $text;
?>
I need to parse readme.md file content using php to display content of my readme.md file.
for example I have below link for my readme.md raw file on github.
https://raw.githubusercontent.com/makadiyaharesh/CLImageEditor/master/README.md
I already fetch the content of file but need to display on my website using PHP code.
Currently when i display this content that is not formatted.
Please help me to do that.
1) Read the full file content.
2)Display that content using php and display in proper formatted view as show on github.
Sorry for my english.
Please help...
Thanks in advance...
.md files uses markdown syntax. you will need a markdown parser for this.
check out - parsedown
hope it will help.
include('Parsedown.php');
$contents = file_get_contents('README.md');
$Parsedown = new Parsedown();
echo $Parsedown->text($contents);
I can convert html page to pdf and send it via email with out any problem, but I am facing trouble with converting php file to pdf.
is it possible to convert php file to pdf using mpdf or do I need to use some other php class for this?
Thanks!
The option seems to be like, first convert the output of the php file to html file, save it and pass that file to mpdf.
Thought my solution may seem other way round but it worked well for me.
Or otherwise this Link
<?php
$file = '/home/user/Desktop/myfile.html';
$result = file_get_contents("url/of/ur/page");
echo $result; //view source now
file_put_contents($file, $result);
?>
now u can pass this file to mpdf. Realie sorie bt i havnt used mpdf till date. May be this solution works for you.
Also, other option is curl.
Somebody has asked me to make an app in php that will generate a .doc file with an image and a few tables in it. My first approach was:
<?php
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
$file = 'new.doc';
$fh = fopen($file,'w');
$uri = data_uri('pic.png','image/png');
fwrite($fh,'<table border="1"><tr><td><b>something</b></td><td>something else</td></tr><tr><td></td><td></td></tr></table>
<br/><img src="'.$uri.'" alt="some text" />
<br/>
<table border="1"><tr><td><b>ceva</b></td><td>altceva</td></tr><tr><td></td><td></td></tr></table>');
fclose($fh);
?>
This uses the data uri technique of embedding an image.
This will generate an html file that will be rendered ok in web browsers but the image is missing in Microsoft Office Word, at least in the standard setup. Then, while editing the file with Word, i've replace the image with an image from file and Microsoft Word changed the contents of the file into Open XML and added a folder, new_files where he put the imported image (which was a .png), a .gif version of the image and a xml file:
<xml xmlns:o="urn:schemas-microsoft-com:office:office">
<o:MainFile HRef="../new.doc" />
<o:File HRef="image001.jpg" />
<o:File HRef="filelist.xml" />
</xml>
Now this isn't good enough either since i want this to be all kept in a single .doc file.
Is there a way to embed an image in an OpenXML-formatted .doc file?
look here http://www.tkachenko.com/blog/archives/000106.html
<w:pict>
<v:shapetype id="_x0000_t75" ...>
... VML shape template definition ...
</v:shapetype>
<w:binData w:name="wordml://02000001.jpg">
... Base64 encoded image goes here ...
</w:binData>
<v:shape id="_x0000_i1025" type="#_x0000_t75"
style="width:212.4pt;height:159pt">
<v:imagedata src="wordml://02000001.jpg"
o:title="Image title"/>
</v:shape>
</w:pict>
There is PHPWord project to manipulate MS Word from within PHP.
PHPWord is a library written in PHP
that create word documents. No Windows
operating system is needed for usage
because the result are docx files
(Office Open XML) that can be opened
by all major office software.
PHPWord can write them http://phpword.codeplex.com/ (note: its still in Beta. I've used PHpExcel by the same guy a lot... never tried the Word version).
Have a look at the phpdocx library for generating real .docx files rather than html files with a .doc extension
PS the extension should strictly be .docx rather than .doc for Open XML Word 2007 files
OpenTBS can create DOCX (and other OpenXML files) dynamic documents in PHP using the technique of templates.
No temporary files needed, no command lines, all in PHP.
It can add or delete pictures. The created document can be produced as a HTML download, a file saved on the server, or as binary contents in PHP.
It can also merge OpenDocument files (ODT, ODS, ODF, ...)
http://www.tinybutstrong.com/opentbs.php
I would use PHPExcel. It can work with OpenXML too.
Here's the link: http://phpexcel.codeplex.com/