Thumbnails
How do you show users what a template or a created document looks like before they download it? ReportingCloud renders the pages as images: a preview of every template in a template picker, the first page of each invoice in a list, a page overview before a document is sent. Two endpoints do this, one for a document you send with the request and one for a template in your template storage, and both return one image per page in the size and format you choose.
How Thumbnails Work
The two endpoints take the same options and differ in where the document comes from:
| Endpoint | Source of the pages |
|---|---|
| POST /v1/document/thumbnails | A document in the body, as a Base64 string in a JSON string. The formats are DOC, DOCX, PDF, TX, XLSX, RTF and HTML |
| GET /v1/templates/thumbnails | A template in your template storage, named in the required query parameter templateName, which is matched regardless of letter case |
Both take these optional query parameters:
| Parameter | Content |
|---|---|
zoomFactor | The size of the images in percent of the page size, from 10 to 400. The default is 100 |
fromPage | The first page to render, from 1 to the number of pages. The default is the first page |
toPage | The last page to render, up to the number of pages. The default is the last page, and so is 0 |
imageFormat | The image format: PNG, JPG, GIF or BMP, in upper or lower case. The default is PNG |
The response is a JSON array of Base64 strings, one image per page, in page order. If fromPage is greater than toPage, the response is null instead of an array.
A request is refused with 400 Bad Request and a plain-text message in these cases:
- "Template not valid":
zoomFactoris not between 10 and 400, orfromPageis less than 1. - "Page numbers not valid":
fromPageortoPageis greater than the number of pages. - "The image format is not supported":
imageFormatis not one of the four formats. - "No merge data found in body": the body of
document/thumbnailsis empty or is not a JSON string. - "Template doesn't exist in storage": the template name of
templates/thumbnailsis not in the storage.
A zoomFactor, fromPage or toPage that is not an integer is ignored, and its default is used, but one given without a value is refused with 400 and the JSON body {"Message":"The request is invalid."}. An empty imageFormat gets 500 Internal Server Error, and so does a body of document/thumbnails whose JSON string is not Base64. Data in a format that document/thumbnails doesn't support is not refused: it is rendered as plain text.
Both endpoints count against the document quota: each successful request adds one document, however many pages it renders, and a response of null counts as well. They have no test parameter. Plans and Licensing lists the quota of each plan, and every request needs the Authorization header that Authentication describes.
Create Thumbnails of a Document With curl
The following commands render the first three pages of invoice.docx at half their size and save each image as a file:
# Wrap the Base64 string of the document in a JSON string
base64 0 invoice.docx | jq . > document.json
curl "https://api.reporting.cloud/v1/document/thumbnails?zoomFactor=50&fromPage=1&toPage=3&imageFormat=PNG" \
"Authorization: ReportingCloud-APIKey $REPORTINGCLOUD_API_KEY" \
"Content-Type: application/json" \
@document.json \
thumbnails.json
# Decode each Base64 string of the array into page-1.png, page-2.png ...
count=$(jq length thumbnails.json)
for i in $(seq 0 $((count - 1))); do
jq ".[$i]" thumbnails.json | base64 > "page-$((i + 1)).png"
done
The commands work as follows:
base64 -w 0encodes the file on one line, andjq -Rs .makes a JSON string of it, which is the whole body.- The query parameters select the pages, the size and the format. Keep the URL in quotes: the shell would otherwise treat each
&as the end of a command. - The response is an array with one Base64 string per page.
jq -r ".[$i]"takes one element without quotes, andbase64 -ddecodes it into an image file.
Create Thumbnails of a Stored Template With curl
A template in the storage needs no body. The following request renders the first page of the demo template sample_invoice.tx, which every trial account has in its storage, as a JPG image at 50 percent:
curl "https://api.reporting.cloud/v1/templates/thumbnails?templateName=sample_invoice.tx&zoomFactor=50&fromPage=1&toPage=1&imageFormat=JPG" \
"Authorization: ReportingCloud-APIKey $REPORTINGCLOUD_API_KEY" \
thumbnails.json
jq ".[0]" thumbnails.json | base64 > sample-invoice.jpg
A document that you have created with the merge or the convert endpoint doesn't have to be uploaded to the storage first: send it to POST /v1/document/thumbnails.
Create Thumbnails With .NET
The .NET SDK, the NuGet package TXTextControl.ReportingCloud, has a method for each endpoint, among them the two thumbnail endpoints. The following console application renders three pages of a document and the first page of a stored template:
using TXTextControl.ReportingCloud;
ReportingCloud rc = new ReportingCloud(
Environment.GetEnvironmentVariable("REPORTINGCLOUD_API_KEY"));
byte[] document = File.ReadAllBytes("invoice.docx");
// Pages 1 to 3 at a zoom factor of 50 percent
List<string> pages = rc.GetDocumentThumbnails(document, 50, 1, 3, ImageFormat.PNG);
for (int i = 0; i < pages.Count; i++)
{
// Each element is a Base64 string: decode it into the image file
File.WriteAllBytes($"page-{i + 1}.png", Convert.FromBase64String(pages[i]));
}
// The first page of a template in the storage
List<string> preview = rc.GetTemplateThumbnails("sample_invoice.tx", 50, 1, 1, ImageFormat.PNG);
File.WriteAllBytes("sample-invoice.png", Convert.FromBase64String(preview[0]));
The code works as follows:
GetDocumentThumbnailstakes the document as a byte array, then the zoom factor, the first and the last page and anImageFormatvalue:PNG,JPG,GIForBMP. All arguments after the document are optional, with the defaults100,1,0andImageFormat.PNG; a last page of0renders up to the end of the document.GetTemplateThumbnailstakes the template name and the zoom factor, followed by the same optional arguments.- In an application for .NET or .NET Core, both methods return a
List<string>of Base64 strings, whichConvert.FromBase64Stringdecodes. In an application for .NET Framework, the package returns aList<System.Drawing.Image>instead, with images you can save withImage.Save. - A refused request throws an
ArgumentExceptionwhose message is the response body.
In the PHP SDK, the methods are getDocumentThumbnails($documentFilename, $zoomFactor, $fromPage, $toPage, $imageFormat) and getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat). All five arguments are required, page numbers start at 1, and each element of the returned array is the binary data of one image. The Java and Ruby SDKs offer template thumbnails only.
Choose the Size and the Pages
- Size.
zoomFactoris a percentage of the page size, from 10 to 400. A value of 100, the default, renders the page at its full size at 96 dpi, 816 by 1056 pixels for a Letter page; smaller values give smaller images for a preview list. - First page only. For a single preview image, set
fromPage=1&toPage=1, or pass1, 1as the page arguments in the SDKs. - Every page. Leave out
fromPageandtoPage, or settoPage=0, and the array has one image for every page. Every request counts as one document, so rendering all pages at once uses less quota than one request per page. - Page count first. To know how many pages a stored template has before you render it, ask the pagecount endpoint that Managing Templates describes.
Next Steps
- Converting Documents: convert the document itself into another format.
- Managing Templates: upload templates, list them and read their page count.
- Supported File Formats: the formats of each endpoint.
- Errors and Status Codes: what a refused request means.