Page 1 of 1

[Win] SaveAsPDF

Posted: Thu Nov 05, 2015 10:24 am
by Mesa
If you want to save a draw in PDF, you can use the PdfVectorOutput() function with a virtual PDF printer.

But, if you want to save just 1 page in a simple PDF, the PurePDF library is enough.

1) Download PurePDF here http://www.purebasicpower.de/?PurePDF
unzip and make sure that PurePDF.pb and PurePDF_res.pb are in your path (somewhere in "\PurePDF225_PB52X86\Examples\PurePDF")

2) use this code WinPDF.pbi

Code: Select all


#PurePDF_Include=1
XIncludeFile "PurePDF.pb"
DisableExplicit

Procedure SaveAsPDF(FileName$, ImagetoSave, Quality=7, Orientation$="P", Unit$="mm", Format$=#PDF_PAGE_FORMAT_A4, OffSetX=0, OffSetY=0, ResizeToA4=#False)
  ;{ Help
  ; FileName$
  ; Path and name of the pdf file
  ;
  ; ImagetoSave
  ; The image number to save
  ;
  ; Quality
  ; The quality of the picture save as a pdf
  ;
  ; Orientation$
  ; Default page orientation. Possible values are (Case insensitive):
  ; - P Or Portrait
  ; - L Or Landscape
  ; Default value is P.
  ; 
  ; Unit$
  ; User measure unit. Possible values are:
  ; - pt: point
  ; - mm: millimeter
  ; - cm: centimeters
  ; - in: inch
  ; A point equals 1/72 of inch, that is To say 0.35 mm (an inch being 2.54 cm). This is a very
  ; common unit IN typography, font sizes are expressed IN that unit.
  ; Default value is mm.
  ; 
  ; Format$
  ; The format used For pages. It can be either one of the following values:
  ; - #PDF_PAGE_FORMAT_4A0
  ; - #PDF_PAGE_FORMAT_2A0
  ; - #PDF_PAGE_FORMAT_A0
  ; - #PDF_PAGE_FORMAT_A1
  ; - #PDF_PAGE_FORMAT_A2
  ; - #PDF_PAGE_FORMAT_A3
  ; - #PDF_PAGE_FORMAT_A4
  ; - #PDF_PAGE_FORMAT_A5
  ; - #PDF_PAGE_FORMAT_A6
  ; - #PDF_PAGE_FORMAT_A7
  ; - #PDF_PAGE_FORMAT_A8
  ; - #PDF_PAGE_FORMAT_A9
  ; - #PDF_PAGE_FORMAT_A10
  ; - #PDF_PAGE_FORMAT_B0
  ; - #PDF_PAGE_FORMAT_B1
  ; - #PDF_PAGE_FORMAT_B2
  ; - #PDF_PAGE_FORMAT_B3
  ; - #PDF_PAGE_FORMAT_B4
  ; - #PDF_PAGE_FORMAT_B5
  ; - #PDF_PAGE_FORMAT_B6
  ; - #PDF_PAGE_FORMAT_B7
  ; - #PDF_PAGE_FORMAT_B8
  ; - #PDF_PAGE_FORMAT_B9
  ; - #PDF_PAGE_FORMAT_B10
  ; - #PDF_PAGE_FORMAT_C0
  ; - #PDF_PAGE_FORMAT_C1
  ; - #PDF_PAGE_FORMAT_C2
  ; - #PDF_PAGE_FORMAT_C3
  ; - #PDF_PAGE_FORMAT_C4
  ; - #PDF_PAGE_FORMAT_C5
  ; - #PDF_PAGE_FORMAT_C6
  ; - #PDF_PAGE_FORMAT_C7
  ; - #PDF_PAGE_FORMAT_C8
  ; - #PDF_PAGE_FORMAT_C9
  ; - #PDF_PAGE_FORMAT_C10
  ; - #PDF_PAGE_FORMAT_RA0
  ; - #PDF_PAGE_FORMAT_RA1
  ; - #PDF_PAGE_FORMAT_RA2
  ; - #PDF_PAGE_FORMAT_RA3
  ; - #PDF_PAGE_FORMAT_RA4
  ; - #PDF_PAGE_FORMAT_SRA0
  ; - #PDF_PAGE_FORMAT_SRA1
  ; - #PDF_PAGE_FORMAT_SRA2
  ; - #PDF_PAGE_FORMAT_SRA3
  ; - #PDF_PAGE_FORMAT_SRA4
  ; - #PDF_PAGE_FORMAT_LETTER
  ; - #PDF_PAGE_FORMAT_LEGAL
  ; - #PDF_PAGE_FORMAT_EXECUTIVE
  ; - #PDF_PAGE_FORMAT_FOLIO
  ; 
  ; OffSetX=0, OffSetY=0
  ; Image offset inside the pdf 
  ;
  ; ResizeToA4=#False
  ; Resize the image to an A4 european format 21mm x 297mm
  ;}
  
  Protected *mem, size;, NbImgH, NbImgV
  
  UseJPEGImageEncoder()
  UseJPEGImageDecoder()
  
  If ResizeToA4 <> #False
    If Orientation$="P" 
      ResizeImage(ImagetoSave, 600, 848)
    Else
      ResizeImage(ImagetoSave, 848, 600)
    EndIf
  EndIf 
  
  ;{ TODO Image bigger than an A4 ?
  ;   If ImageWidth(ImagetoSave)>600
  ;     NbImgH=ImageWidth(ImagetoSave)/600 
  ;     If ImageWidth(ImagetoSave) % 600 >0
  ;       NbImgH= NbImgH + 1
  ;       EndIf
  ;   EndIf
  ;     If ImageHeight(ImagetoSave)>848
  ;     NbImgV=ImageWidth(ImagetoSave)/848 
  ;     If ImageWidth(ImagetoSave) % 848 >0
  ;       NbImgV= NbImgV + 1
  ;       EndIf
  ;   EndIf
  ;} 
  
  
  *mem=EncodeImage(ImagetoSave,#PB_ImagePlugin_JPEG, Quality)  
  
  size= MemorySize(*mem)
  pdf_Create(Orientation$, Unit$, Format$)
  pdf_AddPage()
  
  ;{ Margins
  ;Scale factor, A point equals 1/72 of inch, that is to say 0.35 mm (an inch = 2.54 cm)
  ;   Select Unit$
  ;     Case "pt"
  ;       pdfK = 1
  ;     Case "mm"
  ;       pdfK = 72/25.4
  ;     Case "cm"
  ;       pdfK = 72/2.54
  ;     Case "in"
  ;       pdfK = 72
  ;     Default
  ;       pdfK = 72/25.4
  ;   EndSelect
  ;}
  ;By default margins equal 1 cm = 72/2.54 = 28.35 => pdf_SetMargins(28.35/pdfK, 28.35/pdfK)
  ;Let's set margins to 0 mm
  pdf_SetMargins(0.0, 0.0) 
  
  pdf_ImageMem("littlepic.jpg",*mem,size, OffSetX, OffSetY);
  
  pdf_Save(FileName$)
  
  FreeMemory(*Mem)
  
EndProcedure
An example:

Code: Select all

;==============================================
; To use SaveAsPDF function, just do 4 things:
;==============================================

; ===> 1) Include this file. Note: 2 files from PurePDF have to be in the path, "PurePDF.pb" and "PurePDF_res.pb"
XIncludeFile "WinPDF.pbi"


Enumeration 
  #MainForm
  #Img
  #MainImg
EndEnumeration

Procedure Draw()
  ; From microdevweb: http://www.purebasic.fr/french/viewtopic.php?f=6&t=15600
  Protected X,Y
  StartVectorDrawing(ImageVectorOutput(#Img))
  
  VectorSourceColor($C0C0C0FF)
  FillVectorOutput()
  ;{ Contour
  TranslateCoordinates(-100, 100)
  X=200
  Y=50
  MovePathCursor(X,Y)
  AddPathLine(350,30,#PB_Path_Relative)
  AddPathLine(-40,100,#PB_Path_Relative)
  AddPathLine(-25,0,#PB_Path_Relative)
  AddPathLine(-30,80,#PB_Path_Relative)
  AddPathLine(110,15,#PB_Path_Relative)
  AddPathLine(-40,110,#PB_Path_Relative)
  AddPathLine(-100,-10,#PB_Path_Relative)
  AddPathLine(-30,80,#PB_Path_Relative)
  AddPathLine(210,30,#PB_Path_Relative)
  AddPathLine(-40,80,#PB_Path_Relative)
  AddPathLine(-350,-40,#PB_Path_Relative)
  AddPathLine(70,-180,#PB_Path_Relative)
  AddPathLine(-100,-15,#PB_Path_Relative)
  AddPathLine(40,-110,#PB_Path_Relative)
  AddPathLine(100,15,#PB_Path_Relative)
  AddPathLine(30,-80,#PB_Path_Relative)
  AddPathLine(-180,-20,#PB_Path_Relative)
  AddPathLine(25,-85,#PB_Path_Relative)
  VectorSourceLinearGradient(200,50,800,600)
  VectorSourceGradientColor($FFFFFFFF,0.0)
  VectorSourceGradientColor($FF0000FF,0.2)
  VectorSourceGradientColor($FF2222B2,0.5)
  
  
  FillPath(#PB_Path_Preserve)
  VectorSourceColor($FF000080)
  
  StrokePath(2)
  ;}
  StopVectorDrawing()
EndProcedure

OpenWindow(#MainForm,0,0,600,848,"Logo Pb",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

; ===> 2) Create an image
CreateImage(#Img,600,848)
; European paper format A4 = Portrait 210mm X 297mm = 600pt X 848pt (1pt = 0.35mm)

; ===> 3) Draw what you want on this image
Draw()
ImageGadget(#MainImg,0,0,600,848,ImageID(#Img))

; ===> 4) Use SaveAsPDF() 
;             SaveAsPDF(FileName$, ImagetoSave, Quality=7, Orientation$="P", Unit$="mm", Format$=#PDF_PAGE_FORMAT_A4, OffSetX=0, OffSetY=0, ResizeToA4=#False)
SaveAsPDF("ok.pdf", #Img)

RunProgram("ok.pdf")

Repeat:Event=WaitWindowEvent():Until Event=#PB_Event_CloseWindow

M.