---
title: "How to Generate and Save Invoice PDF Automatically in Magento 2?"
url: "https://magecomp.com/blog/generate-and-save-invoice-pdf-automatically-in-magento-2/"
date: "2023-12-08T09:51:34+00:00"
modified: "2025-02-13T08:55:16+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 579
reading_time: "3 min read"
summary: "Hello, Magento mates. ?"
description: "Hello, Magento mates. ? Welcome to the MageComp Magento tutorials. Today, in this tutorial, we will learn to&hellip;"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Move Cart Total Below Cart Items in Checkout Page"
    url: "https://magecomp.com/blog/magento-2-move-cart-total-below-cart-items-in-checkout-page/"
  - title: "How to Make User Register but not Login in Magento 2.4?"
    url: "https://magecomp.com/blog/make-user-register-but-not-login-in-magento-2-4/"
  - title: "How to Programmatically Check Whether a Database Table Contains a Specified Column in Magento 2?"
    url: "https://magecomp.com/blog/programmatically-check-database-table-contains-specified-column-magento-2/"
---

# How to Generate and Save Invoice PDF Automatically in Magento 2?

_Published: December 8, 2023_  
_Author: Dhiren Vasoya_  

![generate and save invoice pdf automatically in Magento 2](https://magecomp.com/blog/wp-content/uploads/2023/12/generate-and-save-invoice-pdf-automatically-in-Magento-2-1024x512.webp)

Hello, Magento mates. ?

Welcome to the MageComp [**Magento tutorials**](https://magecomp.com/blog/category/how-to-magento/).

Today, in this tutorial, we will learn to generate an invoice PDF automatically and save the invoice to the server once the invoice is created.

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-2.webp)](https://magecomp.com/services/hire-magento-programmer/)

## What is an Invoice PDF?
An invoice PDF is a digital document typically generated by businesses to formalize and communicate financial transactions with their customers.

Invoices typically include crucial information such as the seller’s and buyer’s details, a unique invoice number, itemized descriptions of products or services, quantities, unit prices, and the total amount due. It also serves as a detailed record of goods or services provided, their corresponding costs, and any applicable taxes or fees.

The use of PDF format ensures that the document retains its intended formatting and can be easily viewed and printed across different devices and platforms. In essence, an invoice PDF is a professional and organized way to request payment for goods or services rendered.

**[Free Invoice Generator](https://www.zintego.com/free-invoice-generator)** simplifies billing processes by offering customizable invoice templates. It streamlines the creation of professional invoices, enabling businesses to efficiently manage transactions. With user-friendly interfaces and diverse features, it ensures hassle-free invoicing for freelancers, small businesses, and entrepreneurs.

## Steps to Generate and Save Invoice PDF
**Step 1 –**

First, we need to create an **“events.xml”** file inside our extension at the following path.

**app\code\Vendor\Extension\etc\events.xml**

```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="sales_order_invoice_register">
    <observer name="yourObserverName" instance="VendorExtensionObserverCustomObserver" />
  </event>
</config>
```

**Step 2 –**

After creating the file and entering the code mentioned above, we need to create a **“CustomObserver.php”** file inside our extension at the following path.

**app\code\Vendor\Extension\Observer\CustomObserver.php**

And add the code as given below

```
<?php
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;

class CustomObserver implements ObserverInterface
{
    protected $_pdfInvoiceModel;
    protected $_outputDirectory;
    private $_myPdfStorageSubDirectory = "pdfinvoices";

    public function __construct(
        MagentoSalesModelOrderPdfInvoice $pdfInvoiceModel,
        MagentoFrameworkFilesystem $filesystem
        ) {
            $this->_pdfInvoiceModel = $pdfInvoiceModel;
            $this->_outputDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);
    }

    public function execute(MagentoFrameworkEventObserver $observer)
    {
        try{
            $order = $observer->getData('order');
            if (!$order->hasInvoices()){
               return $this;
            }
            $invoice =  $order->getInvoiceCollection()->getFirstItem();
            $pdfContent = $this->_pdfInvoiceModel->getPdf([$invoice])->render();
            //invoice pdf file created under var directory with IncrementId.pdf file name.
            $this->_outputDirectory->writeFile($this->_myPdfStorageSubDirectory. "/" . $invoice->getIncrementId() . ".pdf" ,$pdfContent);
        } catch (Exception $e){

        }
        return $this;
    }
}
?>
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-2.webp)](https://magecomp.com/magento-malware-removal.html)

**Step 3 –**

After completing the task of entering the code, you have to run the commands mentioned below:

```
php bin/magento setup:di:compile
php bin/magento cache:flush
```

## Conclusion
Code coded smoothly. ?

The efficiency that an automated system of creating and saving invoice PDFs saves the process of invoice generation is huge and accurate. Through use of this Magento technology, these tasks can easily be done eliminating errors, less manual work and overall time taken in coming up with the invoices.
Using this code, it will be easier to create and save invoice PDFs in your Magento 2 store. Hope this tutorial was quite useful and simple for you. If you face any issues with any of the steps in this tutorial, don’t hesitate to [contact us](https://magecomp.com/contact) at any time you wish; our professional [Magento developers](https://magecomp.com/contact) are always here for you.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!!!**


---

_View the original post at: [https://magecomp.com/blog/generate-and-save-invoice-pdf-automatically-in-magento-2/](https://magecomp.com/blog/generate-and-save-invoice-pdf-automatically-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-07 05:31:25 UTC_  
