How To Get Invoice As PDF From Quickbooks Online Using C#

In this blog, I will explain to you how to get an invoice as PDF and download it in .NET MVC web application using SDK.

Before using any Quickbooks online API we need access token, if you don’t know how to get access token then you can find it here.

We will download Invoice pdf by Invoice Id.

For that, we have to get invoice form Quickbooks Online and call GetPdf function. it will return a byte array of pdf, we have to convert byte array to a pdf file and return pdf file to a user.

Below are a few steps to get an invoice as PDF,

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • We have to get the invoice from Quickbooks online by calling Invoice API.
  • We are querying an invoice by ID.
  • For querying/get invoice we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We will get Invoice details in objInvoiceFound if there is an invoice that exists with ID.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add objInvoiceFound object in dataService.GetPdf<Invoice>() for getting an invoice pdf byte Array.
  • dataService.GetPdf<Invoice>() will return byte Array of pdf, we need to convert it into a pdf file for returning the file to a user.
  • At the last, we return pdf File with a unique name.
  • code is as below,
public FileResult GetInvoicePdfById(string InvoiceID)
{
  byte[] InvoicePdfByteArray;
  try
  {
    OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(Access_token);

    // Create a ServiceContext with Auth tokens and realmId
    ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oauthValidator);
    serviceContext.IppConfiguration.MinorVersion.Qbo = "23";
    serviceContext.IppConfiguration.BaseUrl.Qbo = QboBaseUrl;

    string EXISTING_INVOICE_QUERYBYID = string.Format("select * from Invoice where id = '{0}'", InvoiceID);
    var queryService = new QueryService<Invoice>(serviceContext);
    Invoice objInvoiceFound = queryService.ExecuteIdsQuery(EXISTING_INVOICE_QUERYBYID).FirstOrDefault<Invoice>();

    //If Invoice found on Quickbooks online
    if (objInvoiceFound != null)
    {
        DataService dataService = new DataService(serviceContext);
        //it will return Byte Array of pdf
        InvoicePdfByteArray = dataService.GetPdf<Invoice>(objInvoiceFound);
        if (InvoicePdfByteArray != null)
        {
            //This is for unique name format like-"INVOICE-148-11_05_2019114648".
            var CurrentDate = DateTime.Now;
            var FileName = string.Format("{0}-{1}-{2}{3}{4}{5}{6}", "INVOICE", InvoiceID, CurrentDate.ToString("MM/dd/yyyy"), CurrentDate.ToString("HH"), CurrentDate.ToString("mm"), CurrentDate.ToString("ss"), ".pdf");
            return File(InvoicePdfByteArray, "application/pdf", FileName);
        }
    }
    return null;
  }
  catch (IdsException ex)
  {
    return null;
  }
    catch (Exception ex)
  {
    return null;
  }
}
  • The above code will return the pdf file of the particular invoice.

You can set a download link in the list view of the invoice. if you don’t know how to get Invoices from Quickbooks Online you can find it here.

  • View Code(cshtml) of the invoice list with the download link is as below.
@model List<Intuit.Ipp.Data.Invoice>

@{
    ViewBag.Title = "GetAllInvoice";
}

<h2>Quickbooks online Invoice List</h2>

<div>
    <table class="table table-bordered">
        <tr>
            <th>QBO ID</th>
            <th>Invoice Number</th>
            <th>Invoice Date</th>
            <th>Customer</th>
            <th>Total Amount</th>
            <th style="text-align:center;">Download</th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Id</td>
                <td>@item.DocNumber</td>
                <td>@item.TxnDate.ToString("MM/dd/yyyy")</td>
                <td>@item.CustomerRef.name</td>
                <td>@item.TotalAmt</td>
                <td style="text-align:center;"><a href="@Url.Action("GetInvoicePdfById", "Home", new { InvoiceID = item.Id })" target="_blank"><img src="~/Content/downloadICON.png" height="25" width="25" /></a></td>
            </tr>
        }

    </table>
</div>
  • In the above code, on click of download icon invoice pdf will be downloaded.

OUTPUT:

  • Invoice List

  • Invoice pdf

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories