Solved

Convert Binary Data to PDF

Posted on 2016-07-19
8
38 Views
Last Modified: 2016-09-28
Good day all,

I have a field in a SQL table that contains binary data that I need to convert to a PDF. All recommendations are appreciated.
0
Comment
Question by:EMCIT
  • 3
  • 3
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41719533
and what is that binary data?
and do you plan to "display" the data/pdf?
0
 
LVL 11

Author Comment

by:EMCIT
ID: 41719550
The binary is a converted PDF.  I need to convert it back to pdf to save as a .pdf.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41719565
What is the data type of the field that the binary data (PDF data) is stored in?

~bp
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 11

Author Comment

by:EMCIT
ID: 41719567
The field is varbinary(MAX)
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41719597
Okay, what do you have so far?  Are you working in Access using VBA, or in VBScript?  Can you share the code you have that connects to the data base and does a query, etc?

~bp
0
 
LVL 11

Author Comment

by:EMCIT
ID: 41719603
I'm building an Access front end (VB) that will connect to SQL via ODBC. A variable will be passed that will identify the record to be retrieved. The PDF (Binary) will be saved to a network location.
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points (awarded by participants)
ID: 41719628
Okay, here are a couple of examples of what you need to do I believe. The basic idea is to query the table as you would normally to get the column that has the binary data, then store that in a file using an ADO stream.  Hope these help.

See routines shared here:

https://usefulgyaan.wordpress.com/2014/09/30/store-and-fetch-files-sql-server-tables/

I also found this code snippet that is the same idea:

Dim cn  As ADODB.Connection
Dim rs  As ADODB.Recordset
Dim sql As String
Dim oStream As ADODB.Stream

Set cn = New ADODB.Connection

'Here I use default admin user 'sa' and password is blank
cn.Open "Provider = sqloledb;" & _
        "Data Source=ServerNameOrIP;" & _
        "Initial Catalog=DBName;" & _
        "User ID=sa;" & _
        "Password=;"""

'your sql statment including varbinary max field here it is FILEDATA
sql = " select EMAILID,EMAILFROM,EMAILTO,EMAILSUBJECT,FILEDATA from Tbl "


Set rs = New ADODB.Recordset

rs.Open sql, cn

Do Until rs.EOF
    Set oStream = New ADODB.Stream
    With oStream
         .Type = adTypeBinary
         .Open
         .Write rs.Fields(4).Value
         'Here I use 1st field value as file name i.e. rs.fiedls(0).value 
         'In addition you can join drive and/or folder path to save another location
         .SaveToFile rs.Fields(0).Value & ".pdf", adSaveCreateOverWrite
         .Close
    End With
    Set oStream = Nothing
    rs.MoveNext
Loop

Open in new window

~bp
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question