Generating PDFs In Salesforce: What Can Go Wrong And How To Do It Right
Generating PDFs in Salesforce sounds like a small task. At first, it feels straightforward: create a Visualforce page, use renderAs="pdf", generate the file with Apex, and attach it to a record.
Simple.
But in real-world projects, PDF generation often becomes more complex than expected. What starts as a “quick feature” can turn into a performance issue or a maintenance challenge if not designed properly.
One of the first things teams encounter is styling limitations. Salesforce PDF rendering does not fully support modern CSS, which means:
-
Complex layouts can easily break
-
Long text fields may overflow
-
Page breaks can behave unpredictably
-
Small design changes can affect the entire document
This becomes especially problematic when business users request pixel-perfect documents or when new fields are added over time.
Another common issue is performance. Many implementations trigger PDF generation automatically — for example, when a Case is closed or when a record is updated. If the PDF is generated synchronously during the save transaction, it can:
-
Slow down the record save
-
Increase CPU time usage
-
Hit governor limits
-
In extreme cases, block the transaction entirely
This is where a small feature starts impacting overall system stability.
There is also an architectural concern. In many implementations, business logic, data preparation, and rendering are all mixed together. For example:
-
SOQL queries inside the Visualforce page
-
Business rules embedded in rendering logic
-
File attachment logic combined with transaction handling
Over time, this makes the solution harder to test, harder to maintain, and riskier to change.
So what does a better approach look like?
First, separate responsibilities. Prepare and structure the data in an Apex service class, and let the PDF page focus only on presentation. Clean separation makes the system easier to maintain and extend.
Second, avoid blocking critical transactions. If PDF generation is triggered automatically, consider handling it asynchronously so it does not affect the user experience or record save performance.
Third, keep the layout simple. Since styling capabilities are limited, structured tables and controlled page breaks work far better than complex designs.
And finally, recognize when the native solution is not enough. For advanced formatting, digital signatures, or highly customized designs, external PDF services or specialized tools may provide a more scalable and flexible option.
Generating PDFs in Salesforce is not difficult. But generating them correctly — in a way that is stable, scalable, and production-safe — requires thoughtful design decisions from the beginning.
The difference between “it works” and “it works properly” is always in the architecture behind it.