Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

[VB6] Convert PDF and PNG to ZPL

$
0
0
This mdImageConvert.bas module can use pdfium.dll to render PDFs or can use GDI+ to load PNGs and then converts these bitmaps to ZPL's text based Alternative Data Compression scheme which is used by ZPL compatible thermal printers in their graphics printing commands.

Code:

'--- mdImageConvert.bas

Insane forum restrictions do not allow too big posts in CodeBanks for no apparent reason.

Download from https://gist.github.com/wqweto/91872c81ddb3e2ebe13b48f7ea1ce502

Here is a sample snippet which renders PDF to monochrome bitmap on screen and copies final ZPL content to clipboard so it can be previewed at https://labelary.com/viewer.html

Code:

'--- Form1.frm
Option Explicit

Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal hImage As Long) As Long

Private Sub Form_Click()
    Const PDF_FILE      As String = "D:\TEMP\label_base64_pdf.pdf"
    Const DPI          As Long = 203
    Const LABEL_WIDTH  As Single = 108 ' mm        '--- A6 is 105x148 mm
    Const TOP_MARGIN    As Single = 0  ' mm
    Const LEFT_MARGIN  As Single = 3  ' mm
    Const RIGHT_MARGIN  As Single = 3  ' mm
    Dim hImg            As Long
    Dim lDpmm          As Long        ' Dots-Per-Millimeter
    Dim cOutput        As Collection
   
    On Error GoTo EH
    lDpmm = Int(DPI / 25.4 + 0.5)
    hImg = ConvertBitmapToMonochrome(LoadPdfPageToBitmap(ReadBinaryFile(PDF_FILE), (LABEL_WIDTH - LEFT_MARGIN - RIGHT_MARGIN) * lDpmm))
    DrawBitmapToHDC hDC, hImg, 0, 0
   
    Set cOutput = New Collection
    cOutput.Add "^XA"
    cOutput.Add "^FO" & LEFT_MARGIN * lDpmm & "," & TOP_MARGIN * lDpmm
    cOutput.Add "^GF" & ConvertBitmapToZplGraphics(hImg): hImg = 0
    cOutput.Add "^FS"
    cOutput.Add "^PQ1,0,1,Y"
    cOutput.Add "^XZ"
   
    Clipboard.Clear: Clipboard.SetText ConcatCollection(cOutput, vbCrLf)
    Exit Sub
EH:
    If hImg <> 0 Then
        Call GdipDisposeImage(hImg)
    End If
End Sub

Public Function ReadBinaryFile(sFile As String) As Byte()
    Dim baBuffer()      As Byte
    Dim nFile          As Integer

    On Error GoTo EH
    baBuffer = vbNullString
    nFile = FreeFile
    Open sFile For Binary Access Read Shared As nFile
    If LOF(nFile) > 0 Then
        ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
        Get nFile, , baBuffer
    End If
    Close nFile
    ReadBinaryFile = baBuffer
EH:
End Function

Most of pdfium.dll API declares were shamelessly hoisted from PDFiumViewer by Olaf.

cheers,
</wqw>

Viewing all articles
Browse latest Browse all 1529

Trending Articles