Guest User

contao

a guest
Nov 30th, 2017
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Hi Community
  2. I am trying to use the php extension "Imagick" in order to create JPG images from PDF files.
  3. I use Contao 3.5.14
  4. There are two situations where I need this extension to work:
  5. The first situation is when I attach a PDF file to a news article.
  6. In this case, I just create a custom News List module, which I assign a custom template to.
  7. In this custom template, I just throw some php to save my JPG.
  8. It does work fine:
  9. [code]
  10. <div class="publication layout_full block<?= $this->class ?>">
  11. <h2><?= $this->headline ?></h2>
  12. <?php if ($this->hasMetaFields): ?>
  13. <p class="info"><?php echo $this->parseDate("F Y", strtotime($this->date)); ?><?= $this->author ?> <?= $this->commentCount ?></p>
  14. <?php endif; ?>
  15. <?php if ($this->hasSubHeadline): ?>
  16. <h4><?= $this->subHeadline ?></h4>
  17. <?php endif; ?>
  18.  
  19. <?php if ($this->addImage): ?>
  20. <div class='image_container'>
  21. <img src='<?= $this->singleSRC ?>'/>
  22. </div>
  23. <?php endif; ?>
  24.  
  25. <?php if ($this->enclosure): ?>
  26. <div class="enclosure">
  27. <?php for($i=0;$i<count($this->enclosure);$i++) {
  28.  
  29. // the IMAGICK bit:
  30.  
  31. $pdf_file = $this->enclosure[$i]['enclosure'].'[0]';
  32. $save_to = 'files/thumbnails/'.$this->enclosure[$i]['link'].'.jpg';
  33. if (!file_exists($save_to)) {
  34. $im = new Imagick($pdf_file);
  35. $im->setImageFormat ("jpeg");
  36. $im->writeImage ($save_to);
  37. }
  38. ?>
  39. <div class="vignette"><a target="_blank" href="<?= $this->enclosure[$i]['enclosure'] ?>" title="<?= $this->enclosure[$i]['title'] ?> (<?= $this->enclosure[$i]['filesize'] ?>)"><img src='<?= $save_to ?>'/></a></div>
  40.  
  41. <?php } ?>
  42.  
  43. </div>
  44. <?php endif; ?>
  45.  
  46. // and so on...
  47.  
  48. </div>
  49. [/code]
  50. Now here comes the second situation which give me headaches...
  51. I need to use the Imagick extension to create jpg out of PDFs while using the "Download content element".
  52. I modified the ce_download.html5 template in order to add my Imagick bit:
  53. [code]
  54. <?php $this->extend('block_searchable'); ?>
  55. <?php $this->block('content'); ?>
  56. <!--------Here's the IMAGICK bit------------->
  57. <?php
  58. $pdf_file = $this->singleSRC.'[0]';
  59. $save_to = 'files/thumbnails/'.$this->id.'.jpg';
  60. if (!file_exists($save_to)) {
  61. $im = new Imagick($pdf_file);
  62. $im->setImageFormat ("jpeg");
  63. $im->writeImage ($save_to);
  64. }
  65. ?>
  66. <!--------Here's end the IMAGICK bit------------>
  67. <a href="<?= $this->href ?>" title="<?= $this->title ?>">
  68. <div class="image_container">
  69. <img style="width:100%;height:auto" src='<?= $save_to ?>' />
  70. </div>
  71. </a>
  72. <div class="teaser">
  73. <a href="<?= $this->href ?>" title="<?= $this->title ?>">
  74. <h2> <?= $this->link ?> </h2>
  75. </a>
  76. </div>
  77.  
  78. <?php $this->endblock(); ?>
  79. [/code]
  80. And the fatal error thrown in back office when trying to go to the article where I placed my Download element:
  81. [code]
  82. Fatal error: Uncaught exception ImagickException with message unable to open image `files/Folder/myFile.pdf': No such file or directory @ error/blob.c/OpenBlob/2589 thrown in templates/ce_download.html5 on line 11
  83. #0 templates/ce_download.html5(11): Imagick->__construct('files/Folder...')
  84. #1 system/modules/core/library/Contao/BaseTemplate.php(88): include('/home/www/clien...')
  85. #2 system/modules/core/library/Contao/Template.php(277): Contao\BaseTemplate->parse()
  86. #3 system/modules/core/classes/FrontendTemplate.php(46): Contao\Template->parse()
  87. #4 system/modules/core/elements/ContentElement.php(289): Contao\FrontendTemplate->parse()
  88. #5 system/modules/core/elements/ContentDownload.php(72): Contao\ContentElement->generate()
  89. #6 system/modules/core/library/Contao/Controller.php(484): Contao\ContentDownload->generate()
  90. #7 system/cache/dca/tl_content.php(1166): Contao\Controller::getContentElement(Object(Contao\ContentModel))
  91. #8 system/modules/core/drivers/DC_Table.php(4321): tl_content->addCteType(Array)
  92. #9 system/modules/core/drivers/DC_Table.php(378): Contao\DC_Table->parentView()
  93. #10 system/modules/core/classes/Backend.php(650): Contao\DC_Table->showAll()
  94. #11 system/modules/core/controllers/BackendMain.php(131): Contao\Backend->getBackendModule('article')
  95. #12 contao/main.php(20): Contao\BackendMain->run()
  96. #13 {main}
  97. [/code]
  98. line 11 of ce_download.html5 is $im = new Imagick($pdf_file);
  99. I first thought it was a path-related issue but it's not because the JPG is correctly created and displays fine in the Front-end.
  100. So I guess it must be related with the way ce_ templates work and how they display in the back-office.
  101. I really don't know how to get this php code to work without interfering with the ce_download template.
  102. I would be so thankful to anyone who could help me out there.
  103. Regards
  104. Vinny
RAW Paste Data