In this blog, I will explain to you how to get a purchase order 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 purchase order pdf by purchase order Id.

For that, we have to get a “purchase order” 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 a purchase order 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 purchase order from Quickbooks online by calling purchase order API.
  • We are querying a purchase order by ID.
  • For querying/get purchase order we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We will get purchase order details in objPurchaseOrderFound if there is a purchase order that exists with a particular ID.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add objPurchaseOrderFound object in dataService.GetPdf<PurchaseOrder>() for getting a purchase order pdf byte Array.
  • dataService.GetPdf<PurchaseOrder>() 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 GetPurchaseOrderPdfById(string PurchaseOrderID)
{
  byte[] PurchaseOrderPdfByteArray;
  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_PURCHASEORDER_QUERYBYID = string.Format("select * from PurchaseOrder where id = '{0}'", PurchaseOrderID);
    var queryService = new QueryService<PurchaseOrder>(serviceContext);
    PurchaseOrder objPurchaseOrderFound = queryService.ExecuteIdsQuery(EXISTING_PURCHASEORDER_QUERYBYID).FirstOrDefault<PurchaseOrder>();

    //If Purchase Order found on Quickbooks online
    if (objPurchaseOrderFound != null)
    {
        DataService dataService = new DataService(serviceContext);
        //it will return Byte Array of pdf
        PurchaseOrderPdfByteArray = dataService.GetPdf<PurchaseOrder>(objPurchaseOrderFound);
        if (PurchaseOrderPdfByteArray != 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}", "PURCHASEORDER", PurchaseOrderID, CurrentDate.ToString("MM/dd/yyyy"), CurrentDate.ToString("HH"), CurrentDate.ToString("mm"), CurrentDate.ToString("ss"), ".pdf");
            return File(PurchaseOrderPdfByteArray, "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 purchase order.

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

  • View Code(cshtml) of the purchase order list with the download link is as below.
@model List<Intuit.Ipp.Data.PurchaseOrder>
@using Intuit.Ipp.Data;

@{
    ViewBag.Title = "GetAllPurchaseOrder";
}

<h2>Quickbooks online Purchase Order List</h2>

<div>
    <table class="table table-bordered">
        @*<tr style="border: 2px solid black;background-color:#a9a9a9">
                <th>QBO Purchase Order ID</th>
                <th>Purchase Order Number</th>
                <th>Purchase Order Date</th>
                <th>Vendor</th>
                <th>Total Amount</th>
            </tr>*@

        @foreach (var item in Model)
        {
            <tr style="border: 2px solid black;border-bottom: none;background-color:#a9a9a9">
                <th>QBO Purchase Order ID</th>
                <th>Purchase Order Number</th>
                <th>Purchase Order Date</th>
                <th>Vendor</th>
                <th>Total Amount</th>
                <th style="text-align:center;">Download</th>
            </tr>

            <tr style="border: 2px solid black;border-bottom: none;border-top: none;">
                <td>@item.Id</td>
                <td>@item.DocNumber</td>
                <td>@item.TxnDate.ToString("MM/dd/yyyy")</td>
                <td>@item.VendorRef.name</td>
                <td>@item.TotalAmt</td>
                <td style="text-align:center;"><a href="@Url.Action("GetPurchaseOrderPdfById", "Home", new { PurchaseOrderID = item.Id })" target="_blank"><img src="~/Content/Images/downloadICON.png" height="25" width="25" /></a></td>
            </tr>
            <tr style="border: 2px solid black;border-top: none;">
                @*<td>Items Details</td>*@
                <td colspan="7">
                    <table class="table table-bordered">
                        <tr>
                            <td colspan="5" style="text-align:center;"><label>Items Details</label></td>
                        </tr>
                        <tr>
                            <th>Line Id</th>
                            <th>Item Name</th>
                            <th>Qty</th>
                            <th>Total Amount</th>
                            <th>Description</th>
                        </tr>
                        @foreach (var LineItem in item.Line)
                        {
                            <tr>
                                <td>@LineItem.Id</td>
                                @{
                                    ItemBasedExpenseLineDetail Expenseitem = (ItemBasedExpenseLineDetail)LineItem.AnyIntuitObject;
                                }
                                <td>@Expenseitem.ItemRef.name</td>
                                <td>
                                    @if (Expenseitem.QtySpecified)
                                    {
                                        @Expenseitem.Qty
                                    }
                                </td>
                                <td>
                                    @if (LineItem.AmountSpecified)
                                    {
                                        @LineItem.Amount
                                    }
                                </td>
                                <td>@LineItem.Description</td>
                            </tr>
                        }
                    </table>
                </td>
            </tr>
        }

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

OUTPUT:

  • Purchase Order List

  • Purchase Order PDF