1

Background

I am generating a PDF using this visualforce page:

<apex:page controller="ExampleController" renderAs="pdf" applyBodyTag="false">
    <head>
        <style>
            @page { margin: 0.25in; }
        </style>
    </head>
    <apex:outputText value="{!body}" escape="false" />
</apex:page>

And this apex controller:

public without sharing class ExampleController {

    private final String body;

    public ExampleController() {
        body = '<i>html content goes here</i>';
    }

    public String getBody() {
        return body;
    }
}

The PDF file it generates is the usual A4-ish page size.

I would like the PDF to be a small square, let's say:

  • Height: 400px
  • Width: 400px

Question

  1. Is there any way to control the page size of the PDF generated by Salesforce visualforce pages?
1

Specify the page size using the CSS size attribute.

<style>
    @page { margin: 0.25in; size: 4in 4in; }
</style>

You won't find this in the standard Salesforce documentation, but the Flying Saucer guide I linked to should provide a lot of useful information.

| improve this answer | |
  • Annoyingly, I had tried size: 4cm 4cm; and it didn't seem to work... but inches did. thanks – Robs Apr 15 '19 at 20:46
  • @Robs Odd. Might be because VF uses an older version of Flying Saucer? At the very least, you can convert it pretty easily. – sfdcfox Apr 15 '19 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.