Het inladen van een word bestand en omzetten naar pdf
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function document_handler_entity_presave($node) {
if ($node->getEntityType()->id() == 'node' && $node->bundle() == 'documenten') {
require_once('src/Controller/PhpWord/customloader.php');
$file = \Drupal\file\Entity\File::load($node->field_bestand->target_id);
$file_uri = $node->field_bestand->entity->getFileUri();
$url = \Drupal::service('file_system')->realpath($file_uri);
$text = read_docx($url);
$node->set('field_extracted_tekst', $text);
$phpWord = \PhpOffice\PhpWord\IOFactory::load($url);
$dirname = pathinfo($url, PATHINFO_DIRNAME);
$filename = time().'.pdf';
// $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
// $pdf = $wordFile->saveAs($dirname.'/'.$filename, 'HTML');
$pdf = $phpWord->save($dirname.'/'.$filename, 'HTML');
$pdfData = file_get_contents($dirname.'/'.$filename);
// $file = \Drupal::service('file.repository')->writeData($pdfData, 'Public: //'.$filename, FileSystemInterface::EXISTS_RENAME);
$file = file_save_data($pdfData, 'Public: //'.$filename, FileSystemInterface::EXISTS_RENAME);
// var_dump($file);
// die();
$node->set('field_pdf', $file->id());
}
}
if ($node->getEntityType()->id() == 'node' && $node->bundle() == 'documenten') {
require_once('src/Controller/PhpWord/customloader.php');
$file = \Drupal\file\Entity\File::load($node->field_bestand->target_id);
$file_uri = $node->field_bestand->entity->getFileUri();
$url = \Drupal::service('file_system')->realpath($file_uri);
$text = read_docx($url);
$node->set('field_extracted_tekst', $text);
$phpWord = \PhpOffice\PhpWord\IOFactory::load($url);
$dirname = pathinfo($url, PATHINFO_DIRNAME);
$filename = time().'.pdf';
// $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
// $pdf = $wordFile->saveAs($dirname.'/'.$filename, 'HTML');
$pdf = $phpWord->save($dirname.'/'.$filename, 'HTML');
$pdfData = file_get_contents($dirname.'/'.$filename);
// $file = \Drupal::service('file.repository')->writeData($pdfData, 'Public: //'.$filename, FileSystemInterface::EXISTS_RENAME);
$file = file_save_data($pdfData, 'Public: //'.$filename, FileSystemInterface::EXISTS_RENAME);
// var_dump($file);
// die();
$node->set('field_pdf', $file->id());
}
}
Ik haal een geupload word bestand op, en probeer die om te zetten naar pdf. Zoals je kunt zien heb ik al aardig wat geprobeert. $phpWord toont ook gewoon de juiste content uit het word bestand. De pdf word ook aangemaakt. Alleen wil de content niet laden. Heb alleen geen flauw idee waarom.
Ivo P op 20/06/2022 13:03:17:
heb je een pdf-writer (tcpdf) geconfigureerd, zoals bijvoorbeeld op https://github.com/PHPOffice/PHPWord/issues/311
Jup net geprobeert. Ik heb handmatig DomPDF geinstalleerd. Puur omdat ik geen composer online kan gebruiken.
Code (php)
1
2
2
\PhpOffice\PhpWord\Settings::setPdfRendererPath('src/Controller/PhpWord/lib/dompdf/vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
Helaas krijg ik nog steeds de error melding dat er geen pdf render iets is.
Drupal\Core\Entity\EntityStorageException: PDF rendering library or library path has not been defined. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 811 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Toevoeging op 20/06/2022 13:54:59:
Ik heb wat aangepast
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require_once('src/Controller/PhpWord/customloader.php');
$absolutePath = drupal_get_path('module', 'document_handler');
// require_once($absolutePath.'src/Controller/PhpWord/lib/dompdf/vendor/dompdf/dompdf');
$file = \Drupal\file\Entity\File::load($node->field_bestand->target_id);
$file_uri = $node->field_bestand->entity->getFileUri();
$url = \Drupal::service('file_system')->realpath($file_uri);
$text = read_docx($url);
$node->set('field_extracted_tekst', $text);
\PhpOffice\PhpWord\Settings::setPdfRendererPath($absolutePath.'/src/Controller/PhpWord/lib/dompdf/vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($url, 'Word2007');
$dirname = pathinfo($url, PATHINFO_DIRNAME);
$filename = time().'.pdf';
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$test = $xmlWriter->save($dirname.'/'.$filename);
var_dump($test);
die();
$absolutePath = drupal_get_path('module', 'document_handler');
// require_once($absolutePath.'src/Controller/PhpWord/lib/dompdf/vendor/dompdf/dompdf');
$file = \Drupal\file\Entity\File::load($node->field_bestand->target_id);
$file_uri = $node->field_bestand->entity->getFileUri();
$url = \Drupal::service('file_system')->realpath($file_uri);
$text = read_docx($url);
$node->set('field_extracted_tekst', $text);
\PhpOffice\PhpWord\Settings::setPdfRendererPath($absolutePath.'/src/Controller/PhpWord/lib/dompdf/vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($url, 'Word2007');
$dirname = pathinfo($url, PATHINFO_DIRNAME);
$filename = time().'.pdf';
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$test = $xmlWriter->save($dirname.'/'.$filename);
var_dump($test);
die();
En nu krijg ik deze error
The website encountered an unexpected error. Please try again later.
Error: Class 'Dompdf\Dompdf' not found in PhpOffice\PhpWord\Writer\PDF\DomPDF->createExternalWriterInstance() (line 45 of modules/custom/document_handler/src/Controller/PhpWord/lib/PhpOffice/PhpWord/Writer/PDF/DomPDF.php).
Quote:
Error: Class 'Dompdf\Dompdf' not found
Dat lijkt me een duidelijke melding...
Ivo P op 20/06/2022 15:07:53:
Dat lijkt me een duidelijke melding...
Quote:
Error: Class 'Dompdf\Dompdf' not found
Dat lijkt me een duidelijke melding...
Normaal met composer is het veel makkelijker, kon het uiteindelijk oplossen met `require_once($absolutePath.'/src/Controller/PhpWord/lib/dompdf/autoload.inc.php');`