PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.2k stars 2.69k forks source link

Word to PDF conversion issues #1023

Open aayu-ramola opened 7 years ago

aayu-ramola commented 7 years ago

Hello, I am using PHPWord with Cakephp 1.3. I used TCPDF, DomPDF, and MPDF to convert a Resume/CV docx to PDF but the output file is not as expected. On page 2 onwards, it contains a lot of extra spaces, a single word is broken into multiple lines, and images (e.g. logo ) is not coming (pls see attachment). Any tips on getting this to work? Thanks

Below is the code I wrote:

Using MPDF: App::import('Vendor', 'doctopdf/vendor/autoload'); \PhpOffice\PhpWord\Settings::setPdfRendererPath(APP.'vendors'.DS.'mpdf'.DS.'mpdf.php'); \PhpOffice\PhpWord\Settings::setPdfRendererName('MPDF'); $phpWord = \PhpOffice\PhpWord\IOFactory::load($docPath); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF'); $objWriter->save($PdfPath,true);

Using TCPDF: App::import('Vendor', 'doctopdf/vendor/autoload'); \PhpOffice\PhpWord\Settings::setPdfRendererPath(APP.'vendors'.DS.tcpdf'.DS.'tcpdf.php'); \PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); $phpWord = \PhpOffice\PhpWord\IOFactory::load($docPath); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF'); $objWriter->save($PdfPath,true);

Using DomPDF I am getting error : Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'An input file is required (i.e. input_file _GET variable).' in C:\xampp\htdocs\recruiter\app\vendors\dompdf\dompdf.php:199 Stack trace: #0 C:\xampp\htdocs\recruiter\app\vendors\doctopdf\vendor\phpoffice\phpword\src\PhpWord\Writer\PDF\AbstractRenderer.php(89): require_once() #1 C:\xampp\htdocs\recruiter\app\vendors\doctopdf\vendor\phpoffice\phpword\src\PhpWord\Writer\PDF.php(61): PhpOffice\PhpWord\Writer\PDF\AbstractRenderer->construct(Object(PhpOffice\PhpWord\PhpWord)) #2 C:\xampp\htdocs\recruiter\app\vendors\doctopdf\vendor\phpoffice\phpword\src\PhpWord\IOFactory.php(44): PhpOffice\PhpWord\Writer\PDF->construct(Object(PhpOffice\PhpWord\PhpWord)) #3 C:\xampp\htdocs\recruiter\app\controllers\candidates.php(9755): PhpOffice\PhpWord\IOFactory::createWriter(Object(PhpOffice\PhpWord\PhpWord), 'PDF') #4 C:\xampp\htdocs\recruiter\app\controllers\candidates.php(8590): CandidatesController->convertDocToPdf('C:\xampp\ht in C:\xampp\htdocs\recruiter\app\vendors\dompdf\dompdf.php on line 199

Attachment: Alok_Kumar.pdf

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/43134183-word-to-pdf-conversion-issues?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
levieraf commented 7 years ago

I have the same problem!

levieraf commented 7 years ago

To convert .docx to pdf I am checking the command line doc2pdf, I did the test with this and work ok! also I did a test using the soffice of libreoffice which mean that you need to install the libreoffice. Bc I cannot see a librery on php or python or ruby that convert correctly a docx to pdf.

tantos commented 5 years ago

I got the same problem. I lost the header and styles when converting word to pdf using MPDF, TCPDF or DomPDF.

yjrowe commented 5 years ago

I also have the problem. I got random code and the img became big when converting docx word to pdf using DomPDF. the problem is so egg pain.

moly4565 commented 4 years ago

I got same problem. I lost header footer,radio button when converting word to pdf. pls help me. my code below define('PHPWORD_BASE_DIR', realpath(DIR)); require 'phpoffice/phpword/vendor/autoload.php'; require 'phpoffice/phpword/vendor/tecnickcom/tcpdf/tcpdf.php';

$PdfPath = realpath(DIR).'/phpoffice/phpword/vendor/tecnickcom/tcpdf'; \PhpOffice\PhpWord\Settings::setPdfRendererPath($PdfPath); \PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); //echo $paths=realpath(DIR).'/'.$fpdf1; $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->AddPage(); $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->setPrintHeader(true); $pdf->setPrintFooter(true);

// set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf = \PhpOffice\PhpWord\IOFactory::load(realpath(DIR).'/'.$file);

$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pdf ,'PDF'); $xmlWriter->save('pdf/'.substr($file,5,-5).'.pdf');

yjrowe commented 4 years ago

I can not solve this problem with php,I just used python. 

------------------ 原始邮件 ------------------ 发件人: "moly4565"<notifications@github.com>; 发送时间: 2019年12月2日(星期一) 晚上6:24 收件人: "PHPOffice/PHPWord"<PHPWord@noreply.github.com>; 抄送: "罗永均"<417969832@qq.com>;"Comment"<comment@noreply.github.com>; 主题: Re: [PHPOffice/PHPWord] Word to PDF conversion issues (#1023)

I got same problem. I lost header footer,radio button when converting word to pdf. pls help me. my code below define('PHPWORD_BASE_DIR', realpath(DIR)); require 'phpoffice/phpword/vendor/autoload.php'; require 'phpoffice/phpword/vendor/tecnickcom/tcpdf/tcpdf.php';

$PdfPath = realpath(DIR).'/phpoffice/phpword/vendor/tecnickcom/tcpdf'; \PhpOffice\PhpWord\Settings::setPdfRendererPath($PdfPath); \PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); //echo $paths=realpath(DIR).'/'.$fpdf1; $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->AddPage(); $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->setPrintHeader(true); $pdf->setPrintFooter(true);

// set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf = \PhpOffice\PhpWord\IOFactory::load(realpath(DIR).'/'.$file);

$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pdf ,'PDF'); $xmlWriter->save('pdf/'.substr($file,5,-5).'.pdf');

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

yvescleuder commented 4 years ago

I problem.. not converting header..

mpolch commented 3 years ago

I am having problems with tabulators. They are just moved to the left..

Asmitta-01 commented 1 year ago

I am having problems with tabulators. They are just moved to the left..

I have the same problem.

Asmitta-01 commented 1 year ago

Anyone has a tip for it please ?