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

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

    //If Estimate found on Quickbooks online
    if (objEstimateFound != null)
    {
        DataService dataService = new DataService(serviceContext);
        //it will return Byte Array of pdf
        EstimatePdfByteArray = dataService.GetPdf<Estimate>(objEstimateFound);
        if (EstimatePdfByteArray != null)
        {
            //This is for unique name format like-"ESTIMATE-148-11_05_2019_114648".
            var CurrentDate = DateTime.Now;
            var FileName = string.Format("{0}-{1}-{2}_{3}{4}{5}{6}", "ESTIMATE", EstimateID, CurrentDate.ToString("MM/dd/yyyy"), CurrentDate.ToString("HH"), CurrentDate.ToString("mm"), CurrentDate.ToString("ss"), ".pdf");
            return File(EstimatePdfByteArray, "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 estimate.

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

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

@{
    ViewBag.Title = "GetAllEstimate";
}

<h2>Quickbooks online Estimate List</h2>

<div>
    <table class="table table-bordered">
        <tr>
            <th>QBO ID</th>
            <th>Estimate Number</th>
            <th>Due 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("GetEstimatePdfById", "Home", new { EstimateID = item.Id })" target="_blank"><img src="~/Content/Images/downloadICON.png" height="25" width="25" /></a></td>
            </tr>
        }

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

OUTPUT:

  • Estimate List

  • Estimate pdf