rinkesh-prajapat's avatar

How i can convert doc to pdf in laravel

i want to convert doc file to pdf in laravel 5.6

0 likes
9 replies
rinkesh-prajapat's avatar

it given :- PDF rendering library or library path has not been defined.

lostdreamer_nl's avatar

Installing Dom PDF and setting it up in PhpWord should do the trick:

composer require dompdf/dompdf

Then in the part where you use PhpWord to create the PDF, first do this:

$domPdfPath = base_path( 'vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
n0urddine's avatar

@lostdreamer_nl bro when i convert docx to pdf with this its working but without docx style and arabic not working.... , its just converted with losing style

public function attr(Request $request)
{

   /* Set the PDF Engine Renderer Path */
   $domPdfPath = base_path('vendor/dompdf/dompdf');
   \PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
   \PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');

   $template = new TemplateProcessor(storage_path('app/public/docs/AR.docx'));

    // Get the form data
    $title = $request->input('title');

    // Replace placeholders with form data
    $template->setValue('title', $title);


    /*@ Save Temporary Word File With New Name */
    $name = uniqid();
    $saveDocPath = storage_path('app/public/docs/AR_'.$name.'.docx');
    $template->saveAs($saveDocPath);

    // Load temporarily create word file
    $Content = \PhpOffice\PhpWord\IOFactory::load($saveDocPath);

    //Save it into PDF
    $savePdfPath = storage_path('app/public/pdfs/attr/AR_'.$name.'.pdf');

     /*@ If already PDF exists then delete it */
     if ( file_exists($savePdfPath) ) {
        unlink($savePdfPath);
    }

    //Save it into PDF
    $PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');
    $PDFWriter->save($savePdfPath); 
    echo 'File has been successfully converted';

    /*@ Remove temporarily created word file */
    if ( file_exists($saveDocPath) ) {
        unlink($saveDocPath);
    }
    return response()->download($savePdfPath, 'AR.pdf');
}
1 like
Ali_Yazdanifar's avatar

@n0urddine Hello, I have the same problem as you and I am having trouble with Persian words. Farsi words in the form of "?" are created Have you found a solution?

1 like
ammarax's avatar

i have problem convert from template, the style css and image will broken if i start from a doc generatted in word... no one too?

masifkhan's avatar

The conversion of docx to pdf is working fine, i am trying to convert .doc to .pdf but this has some issues. It is not loading the original file to convert to pdf. Any one has fixed it?

Please or to participate in this conversation.