I have a visualforce page renderas="PDF".

But I need to attach this pdf to a record in custom object.

I have custom button to open this pdf but this is just enabled if the checkbox field in record=true.

How can I create this button just if checkbox = true and attach the pdf?

If can help me I appreciate.


2 Answers 2


On the attaching the PDF part of your question, you can connect your button to a controller method as described in e.g. Call Apex Class From A Custom Button.

To generate the PDF and attach it you can use the getContentAsPDF method and insert the Attachment:

global class MyClass {
    webservice static void attachPdf(Id customObjectId) {
        PageReference pr = Page.YourPageName;
        // Add parameters (if any) passed from the custom button or queried locally
        pr.getParameters().put('id', ...);
        insert new Attachment(
                ParentId = customObjectId,
                Name = 'Whatever Name You Want',
                Body = pr.getContentAsPDF(),
                ContentType = 'application/pdf'

I made a "Content Document" so I could then create a public link and use it in the front:

public class PX_IRN_PDF_CTR {
    public  static String generaPDF( Id accId1){

        String url = '/apex/PX_IRN_PAGE?id=' + accId1;
        PageReference pageRef = new PageReference(url);
        Blob ret = pageRef.getContentAsPDF();
        String base64 = EncodingUtil.base64Encode(ret);
        String  respuesta='';

        ContentVersion v = new ContentVersion();
        v.versionData = EncodingUtil.base64Decode(base64);
        v.title = 'irn.pdf';
        v.pathOnClient ='irn.pdf';
        insert v;
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:v.Id].ContentDocumentId;
        ContentDocumentLink cDe = new ContentDocumentLink();
        cDe.ContentDocumentId = conDoc;
        cDe.LinkedEntityId = accId1;
        cDe.ShareType = 'I';
        cDe.Visibility = 'AllUsers';
        insert cDe;  

        ContentVersion cv = [select id from contentversion where contentdocumentid =:conDoc];
        respuesta =cv.id;
        ContentDistribution cd = new ContentDistribution();
        cd.Name = 'IRNPDF';
        cd.ContentVersionId =  cv.id;
        cd.PreferencesAllowViewInBrowser= true;
        cd.PreferencesAllowOriginalDownload= true;

        insert cd;

        String var3 = [SELECT DistributionPublicUrl FROM ContentDistribution  WHERE Id =:cd.Id].DistributionPublicUrl;

        return var3;

  • 2
    Hola Benjamín, bienvenido! En este sitio se usa el inglés exclusivamente. Me tomé la libertad de traducir tu post para que todos puedan leerlo. Nos vemos! May 11, 2020 at 20:40
  • @Benjamin Good example code for the new file system. As a minor tip I'd like to mention that you can automatically create the content document link if you set the FirstPublishLocationId to the record it has to be related to. (This only works on creation.)
    – Kasper
    May 11, 2020 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .