Sample code for 30+ languages & platforms
SQL Server

PDF File Encoding to Base64

See more Base64 Examples

Demonstrates how to encode a PDF file to base64, and then decode.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @success int
    SELECT @success = 0

    DECLARE @pdfData int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @pdfData OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdfData, 'LoadFile', @success OUT, 'qa_data/helloWorld.pdf'
    IF @success <> 1
      BEGIN

        PRINT 'failed to load PDF file.'
        EXEC @hr = sp_OADestroy @pdfData
        RETURN
      END

    -- Encode the PDF to base64
    -- Note: to produce base64 on multiple lines (as it would appear in the MIME of an email),
    -- pass the string "base64_mime" instead of "base64".
    DECLARE @b64 nvarchar(4000)
    EXEC sp_OAMethod @pdfData, 'GetEncoded', @b64 OUT, 'base64'

    PRINT @b64

    -- Decode from base64 PDF.
    DECLARE @pdfData2 int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @pdfData2 OUT

    EXEC sp_OAMethod @pdfData2, 'AppendEncoded', @success OUT, @b64, 'base64'
    EXEC sp_OAMethod @pdfData2, 'WriteFile', @success OUT, 'qa_output/helloWorld2.pdf'
    IF @success <> 1
      BEGIN

        PRINT 'failed to write PDF file.'
        EXEC @hr = sp_OADestroy @pdfData
        EXEC @hr = sp_OADestroy @pdfData2
        RETURN
      END

    EXEC @hr = sp_OADestroy @pdfData
    EXEC @hr = sp_OADestroy @pdfData2


END
GO