Skip to content

[4.x]: Can not find a PDF to generate URL exception thrown on order complete page when no PDFs are set up in Commerce #4195

@paulstone

Description

@paulstone

What happened?

Description

After updating to the latest version of Craft Commerce 4.x, we were getting an error on a client's site after the user had successfully completed an order.

We are pretty much using the default templates, so we were using the same conditional as you can see in /vendor/craftcms/commerce/example-templates/dist/shop/customer/order.twig template, at line 35:

{% if order.pdfUrl and craft.commerce.pdfs.hasEnabledPdf %}

We don't have a default PDF set up (or any PDFs) for this site, and so this exception was being triggered in src/services/Pdfs.php at line 450:

public function getPdfUrl(Order $order, string $option = null, string $pdfHandle = null, bool $inline = false): string
{
// Load the PDF to get its link expiry setting
if ($pdfHandle) {
$pdf = $this->getPdfByHandle($pdfHandle);
} else {
$pdf = $this->getDefaultPdf();
}
if (!$pdf) {
throw new \InvalidArgumentException("Can not find a PDF to generate URL.");

I guess it was silently passing over it before, but the updated code in services/Pdfs.php is now expecting/requiring a PDF?

To get around the issue I've simply commented out that code in the orders.twig template

Steps to reproduce

  1. Install Craft CMS v4 and Commerce v4.
  2. Use default templates
  3. Do not create a default PDF in Commerce's settings.
  4. Go through the order/checkout process.
  5. Exception mentioned above is thrown.

Expected behavior

Order page is shown, but without a 'Download PDF' link.

Actual behavior

Exception mentioned above is thrown.

Thanks. Please let me know if there's something I am missing, or if you require further information.

Craft CMS version

Craft Pro 4.16.17

Craft Commerce version

4.10.0

PHP version

8.2

Operating system and version

Linux 6.1.0-35-amd64

Database type and version

MySQL 28.1

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.1-41)

Installed plugins and versions

Relevant plugins:

Craft Commerce 4.10.0
PayPal Checkout for Craft Commerce 2.1.5
Stripe for Craft Commerce 4.1.8

Other plugins omitted.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions