Blob Class

Contains methods for the Blob primitive data type.

Namespace

System

Usage

Salesforce supports Blob manipulation only with Apex class methods that are supplied by Salesforce. For more information on Blobs, see Primitive Data Types.

Blob Methods

The following are methods for Blob.

size()

Returns the number of bytes in the Blob.

Signature

public Integer size()

Return Value

Type: Integer

Example

1String myString = 'StringToBlob';
2Blob myBlob = Blob.valueof(myString);
3Integer size = myBlob.size();

toPdf(stringToConvert)

Creates a binary object out of the given string, encoding it as a PDF file.

Signature

public static Blob toPdf(String stringToConvert)

Parameters

stringToConvert
Type: String

Return Value

Type: Blob

Usage

Blob.toPDF(stringToConvert) works with any string value. Since the Spring ’26 release, Blob.toPDF() can use the same PDF rendering service as Visualforce. This change is currently controlled by a Release Update. See Use Visualforce PDF Rendering Service with Apex Blob.toPdf() (Release Update) in the Salesforce release notes.

See Render a Visualforce Page as a PDF File for details of the improved PDF rendering service, including considerations and limitations for rendering PDF files.

The Visualforce PDF rendering service expands the range of fonts available, and includes a multibyte-capable font. The default font is serif, which is a change from the default sans-serif used by Blob.toPDF(). See Fonts Available When Using Visualforce PDF Rendering.

Example

1String pdfContent = 'This is a test string';
2Account a = new account(name = 'test');
3insert a;
4Attachment attachmentPDF = new Attachment();
5attachmentPdf.parentId = a.id;
6attachmentPdf.name = a.name + '.pdf';
7attachmentPdf.body = Blob.toPDF(pdfContent);
8insert attachmentPDF;

toString()

Casts the Blob into a String.

Signature

public String toString()

Return Value

Type: String

Example

1String myString = 'StringToBlob';
2Blob myBlob = Blob.valueof(myString);
3System.assertEquals('StringToBlob', myBlob.toString());

valueOf(stringToBlob)

Casts the specified String to a Blob.

Signature

public static Blob valueOf(String stringToBlob)

Parameters

stringToBlob
Type: String

Return Value

Type: Blob

Example

1String myString = 'StringToBlob';
2Blob myBlob = Blob.valueof(myString);