[Win] SaveAsPDF

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Mesa
Messages : 1092
Inscription : mer. 14/sept./2011 16:59

[Win] SaveAsPDF

Message par Mesa »

En attendant le développement du pdf sous PB, voici une astuce pour créer une seule page en PDF.

Le but est de créer une image au format A4 et d'en faire un PDF avec la lib PurePDF
http://www.purebasicpower.de/?PurePDF
Marche sous XP32 mais devrait fonctionner avec PB en 64b aussi, malgré la lib en x86.

Reste à faire, si ça tente quelqu'un, de découper une image trop grosse en plusieurs A4 (facile à faire)

1) Télécharger purePDF
http://www.purebasicpower.de/?PurePDF
Décompresser et allez dans le dossier "PurePDF225_PB52X86\Examples\PurePDF" et vérifier que le PurePDF.pb est bien là

2) Enregistrer le pbi ci dessous dans le même dossier
WinPDF.pbi

Code : Tout sélectionner

#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$)
  
EndProcedure
3) Tester avec ce code, toujours dans le même dossier

Code : Tout sélectionner


XIncludeFile "WinPDF.pbi"


Enumeration 
  #MainForm
  #Img
  #MainImg
EndEnumeration

Procedure Draw()
  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)
CreateImage(#Img,600,848)
; European paper format A4 = Portrait 210mm X 297mm = 600pt X 848pt (1pt = 0.35mm)

Draw()
ImageGadget(#MainImg,0,0,600,848,ImageID(#Img))

SaveAsPDF("ok.pdf", #Img)

RunProgram("ok.pdf")

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

M.
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: [Win] SaveAsPDF

Message par falsam »

Avec la généralisation de Windows 10, la lib PurePDF va devenir obsolète.

■ Si vous utilisez PureBasic 5.40 l'impression de fichier pdf est déja possible sous Mac et Linux avec la fonctionnalité StartVectorDrawing(PdfVectorOutput())

Sous windows par contre cette fonctionnalité ne fonctionne pas encore. Mais ça va venir je n'en doute point :wink:

■ Si vous êtes sous Windows 10, vous avez une imprimante Microsoft PDF installé. Vous pouvez donc utiliser la fonctionnalité StartVectorDrawing(PrinterVectorOutput())

j'ai montré un exemple il y a quelques temps sur ce topic
:arrow: http://www.purebasic.fr/french/viewtopi ... =1&t=15432
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Patrick88
Messages : 1564
Inscription : mer. 21/janv./2004 18:24

Re: [Win] SaveAsPDF

Message par Patrick88 »

sur les windows antérieur à 10, il y a les logiciels
pdffactory (en version shareware, ajoute un liseret pub)
pdfcreator
en libre
CC PDF Converter
....

tous ces trucs ajoute une imprimante virtuelle PDF, suffit de la mettre en imprimante par défaut et tous ce que vous imprimez part dessus, utile pour ce faire des pdf de pages web... etc

Pat
Répondre