From 157701d7e02db39299bbcd370f9d5bbbdbcc5c34 Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Fri, 30 Dec 2022 08:44:33 +1100 Subject: [PATCH] Replace mPDF HTTP Request with wp_remote_get() Resolve issues loading images / assets when using mPDF's native cURL functionality. Fixes #1440 --- README.txt | 5 +- pdf.php | 4 +- src/Helper/Helper_Mpdf_Http_Client.php | 98 ++++++++++++++++++++++++++ src/Helper/Helper_PDF.php | 14 +++- 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/Helper/Helper_Mpdf_Http_Client.php diff --git a/README.txt b/README.txt index 092de41f1..a6497fe66 100644 --- a/README.txt +++ b/README.txt @@ -5,7 +5,7 @@ Donate link: https://gravitypdf.com/donate-to-plugin/ Tags: gravityforms, gravity, forms, pdf, automation, attachment, email Requires at least: 5.3 Tested up to: 6.1 -Stable tag: 6.5.2 +Stable tag: 6.5.3 Requires PHP: 7.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl.txt @@ -107,6 +107,9 @@ If you aren't able to meet the v6 minimum requirements [you can download v5 whic == Changelog == += 6.5.3 = +* Bug: Fix image/asset loading issue due to a cURL error + = 6.5.2 = * Bug: Fix PHP error when a non-string is passed to the Kses sanitizing class * Bug: Resolve memory problem generating Core PDFs if an HTML element contains more than 10+ classes (field CSS Classes are now truncated to 8 user-defined classes) diff --git a/pdf.php b/pdf.php index 5427d7f5c..c7d42f66f 100644 --- a/pdf.php +++ b/pdf.php @@ -1,7 +1,7 @@ logger = $logger; + $this->debug = $debug; + } + + /** + * Make a network request using wp_remote_get() and return a PSR-7 Response + * + * @param RequestInterface $request + * + * @return Response + * @throws MpdfException + * + * @since 6.5.3 + */ + public function sendRequest( RequestInterface $request ) { + if ( null === $request->getUri() ) { + return new Response(); + } + + $url = $request->getUri(); + $this->logger->debug( \sprintf( 'Fetching (wp_remote_get()) content of remote URL "%s"', $url ), [ 'context' => Context::REMOTE_CONTENT ] ); + + $http_call_args = apply_filters( + 'gfpdf_http_request_arguments', + [ + 'timeout' => 10, + ] + ); + $http_call = wp_remote_get( (string) $url, $http_call_args ); + + if ( is_wp_error( $http_call ) ) { + $this->logger->error( $http_call->get_error_message(), [ 'context' => Context::REMOTE_CONTENT ] ); + if ( $this->debug ) { + throw new MpdfException( $http_call->get_error_message() ); + } + + return new Response(); + } + + $headers = wp_remote_retrieve_headers( $http_call ); + $status_code = wp_remote_retrieve_response_code( $http_call ); + $body = wp_remote_retrieve_body( $http_call ); + + $response = new Response( $status_code, $headers->getAll(), $body ); + + if ( $status_code !== 200 ) { + $message = \sprintf( 'HTTP error: %d', $status_code ); + $this->logger->error( $message, [ 'context' => Context::REMOTE_CONTENT ] ); + if ( $this->debug ) { + throw new MpdfException( $message ); + } + } + + return $response; + } + + /** + * @param LoggerInterface $logger + * + * @return void + * + * @since 6.5.3 + */ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; + } +} diff --git a/src/Helper/Helper_PDF.php b/src/Helper/Helper_PDF.php index 9a6c55e1b..fc6d9382b 100644 --- a/src/Helper/Helper_PDF.php +++ b/src/Helper/Helper_PDF.php @@ -4,6 +4,7 @@ use Exception; use GFPDF_Vendor\Mpdf\Config\FontVariables; +use GFPDF_Vendor\Mpdf\Container\SimpleContainer; use GFPDF_Vendor\Mpdf\Mpdf; use GFPDF_Vendor\Mpdf\MpdfException; use GFPDF_Vendor\Mpdf\Utils\UtfString; @@ -619,9 +620,17 @@ public function get_full_pdf_path() { * @since 4.0 */ protected function begin_pdf() { + $options = \GPDFAPI::get_options_class(); + $default_font_config = ( new FontVariables() )->getDefaults(); - $this->mpdf = new Helper_Mpdf( + $http_client = new Helper_Mpdf_Http_Client( $this->log, $options->get_option( 'debug_mode', 'No' ) === 'Yes' ); + $container = new SimpleContainer( + [ + 'httpClient' => $http_client, + ] + ); + $this->mpdf = new Helper_Mpdf( apply_filters( 'gfpdf_mpdf_class_config', [ @@ -658,7 +667,8 @@ protected function begin_pdf() { $this->entry, $this->settings, $this - ) + ), + $container ); $this->mpdf->setLogger( $this->log );