Skip to main content

Working with PDF files in SFCC

  • Salesforce Commerce Cloud

Published

Developer wearing a headset, typing on a laptop next to a monitor full of code

Portable Document Format (PDF) files are some of the most commonly used file types across the web. In eCommerce, that also holds true. Whether it’s for invoices, product manuals, energy labels, or even promotional flyers, there is always a requirement to share them with customers.

When it comes to Salesforce Commerce Cloud (SFCC), you might find that working with PDF files is not as well-documented as it could be. This blog aims to provide a high-level overview and cover the most important considerations for sharing PDF files with customers in Salesforce Commerce Cloud.

File storage and access

Use cases and requirements will dictate where the files are stored and how they can be accessed.

File storage

Separation of responsibilities between complex systems is essential to ensure consistency across them.

  • In the case of invoices, it’s best to let the invoicing system generate the invoice files.
  • For product manuals or energy labels, the system that handles the catalog should expose the files to your system.
  • For promotion flyers, the marketing/content team should provide them.

In smaller systems, that might not be possible, so alternative solutions should be considered. If only storage is needed, SFCC’s library system, WebDAV or a separate file management service can provide that. If you also need to create the PDF files, they can be generated on-the-fly using libraries such as jsPDF (GitHub).

File access

If the document should/can be available to anyone on the internet, such as a general promotion flyer, loyalty program overview, or energy label, then a simple link to the required file should suffice.

Otherwise, file access should be restricted. This can be based on customer level, order level (e.g., invoice), order product line item level (e.g., product manual), or more generally, registered or ordering customer level. This means we cannot expose a direct link to the file but need to serve the file from the server-side.

Additional considerations

GDPR and local legislation might impact your solution, so it is important to anticipate this and ensure compliance when designing the solution.

Exposing the files

If you are storing the files in SFCC’s libraries, you can simply expose the URL to those files. This is very simple - just use the URLUtils class and its staticURL method, and specify the library context.

If, however, you went with a different solution, please continue reading.

It turns out you can expose files using both traditional controller endpoints and Salesforce Commerce API (SCAPI) custom endpoints! This means that it can be accessible to your customers regardless of whether or not you are using SFRA or a headless architecture.

Technically, the approach is very much the same. The only difference is the syntax and some limitations.

Limitations

Before you jump into coding, it is important to consider the file sizes you are working with. Whether using SCAPI custom endpoints or controller endpoints, Salesforce imposes a hard limit of 10 MB on the response size for this type of response. If any of your files exceed that limit, another approach should be used.

Another thing to consider is the average response time, especially if the files are being retrieved from a third-party API. If the third-party API is also generating the file on-demand or is slow in general, beware that:

  • SCAPI requests time out after 10 seconds.
  • Controller endpoints time out after 30 seconds.

Encoding

If you haven't worked with PDF files before, here's a major time-saver: encoding matters!

It is key to serving readable PDF files to the front-end. Based on past experience, the only encoding that worked was “iso-8859-1.”

You can implement it simply by setting the response’s content type as “application/pdf;charset=iso-8859-1”.

Suptilno robovlasništvo zvano Loyalty: Da li nudimo benefite, ili vezujemo kupce lancima?

Working with PDF files in SFCC

Downloading the file

Server-side

First, we need to fetch the PDF. This should be done based on the file storage solution. As the files are most often stored on separate file management systems, this means it should be a simple GET request.

Now we need to expose that file to the client-side. Now is the time to re-read the Encoding paragraph and set the response content type to “application/pdf;charset=iso-8859-1”.

In addition, the “Content-Disposition” header should be present in the response. Its value should be: attachment;filename={filename}.pdf, where {filename} is replaced with the desired file name. This header signals to the system making the request that it should prompt a download with a defined file name.

While this header isn’t strictly needed in our use case (because we’re using XHR such as AJAX, fetch, axios, etc.), it’s included to provide consistency, especially if you want other systems (such as a dedicated mobile app) to download the file as well.

Client-side

A few lines of code are enough to download the file. Generally, we are creating an <a> element in JavaScript, to which we will add a download behavior and perform a simple click:

const downloadPdf = async () => {
const response = await fetchPdf();

response.blob().then((blob) => {
const fileURL = window.URL.createObjectURL(blob);
const alink = document.createElement('a');
alink.href = fileURL;
// The line below actually downloads the PDF with the desired file name
alink.download = 'filename.pdf';
alink.click();
});
}

You just need to make sure that this function is tied to a button click, and that should be it!

Conclusion

Providing PDF files to customers is not as straightforward as it seems, as a lot of considerations need to be made before the implementation even begins. However, we hope this blog has provided enough information to help you get started quickly, saving you time in the process!

Read our latest blogs

Two Bee IT colleagues reviewing something on a laptopSalesforce Commerce Cloud

Salesforce Commerce Explained: D2C and B2B Differences

You are already using Salesforce CRM and want to scale your eCommerce as well, but not sure where to start? Learn more about different Salesforce Commerce options and choose what is best for your long-term goals!

Read more: Salesforce Commerce Explained: D2C and B2B Differences
View all

Tell us your challenge and get actionable insights, completely free.

Cookies on beeit.io

We use necessary storage to make this site work. With your consent, we also use analytics cookies (Google Analytics, Microsoft Clarity) to see how people use the site, and marketing cookies (LinkedIn) to measure our ads and show them to people who visited us. Marketing cookies share data with LinkedIn. You can change your choice at any time under Cookie settings in the footer. Read our cookie policy.