From d3f7b6cc1ad4e29a592225528c1a3e95786c848f Mon Sep 17 00:00:00 2001 From: wizonesolutions <wizonesolutions@739994.no-reply.drupal.org> Date: Sun, 16 Feb 2020 14:42:22 +0100 Subject: [PATCH] Issue #2883481 by wizonesolutions, ovi.indrei, carstenG: PDF file gets downloaded as HTML file --- src/Plugin/FillPdfActionPlugin/FillPdfDownloadAction.php | 2 ++ tests/src/Functional/HandlePdfControllerTest.php | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/Plugin/FillPdfActionPlugin/FillPdfDownloadAction.php b/src/Plugin/FillPdfActionPlugin/FillPdfDownloadAction.php index eb37428..628ff84 100644 --- a/src/Plugin/FillPdfActionPlugin/FillPdfDownloadAction.php +++ b/src/Plugin/FillPdfActionPlugin/FillPdfDownloadAction.php @@ -35,6 +35,8 @@ class FillPdfDownloadAction extends FillPdfActionPluginBase { $this->configuration['filename'] ); $response->headers->set('Content-Disposition', $disposition); + $response->headers->set('Content-Type', 'application/pdf'); + $response->headers->set('Content-Length', strlen($this->configuration['data'])); return $response; } diff --git a/tests/src/Functional/HandlePdfControllerTest.php b/tests/src/Functional/HandlePdfControllerTest.php index ea8431a..c8ad872 100644 --- a/tests/src/Functional/HandlePdfControllerTest.php +++ b/tests/src/Functional/HandlePdfControllerTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\fillpdf\Functional; use Drupal\Core\Url; use Drupal\file\Entity\File; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; /** * @coversDefaultClass \Drupal\fillpdf\Controller\HandlePdfController @@ -35,6 +36,8 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase { /** * Tests DownloadAction. + * + * @throws \Behat\Mink\Exception\ExpectationException */ public function testDownloadAction() { $this->uploadTestPdf('fillpdf_test_v3.pdf'); @@ -58,6 +61,10 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase { $maybe_pdf = $this->getSession()->getPage()->getContent(); static::assertEquals('application/pdf', $this->getMimeType($maybe_pdf), 'The file has the correct MIME type.'); + // Ensure the headers are set to make the PDF download. + $this->assertSession()->responseHeaderContains('Content-Disposition', ResponseHeaderBag::DISPOSITION_ATTACHMENT); + $this->assertSession()->responseHeaderContains('Content-Type', 'application/pdf'); + $this->assertSession()->responseHeaderContains('Content-Length', (string) strlen(file_get_contents($this->getTestPdfPath('fillpdf_test_v3.pdf')))); } /** -- GitLab