Printing

Mac OSX specific forum
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Printing

Post by collectordave »

Starting to get my head round printing using PB. Had some success on windows but I am also new to the MAC. I can get the MAC to print images, text etc in portrait or landscape but keep getting offsets etc.

With a lot of help from this forum I can now enumerate printers on windows and MAC and the paper sizes on the MAC.

In Windows I ceated a structure for printer information as shown below to help with accurate printing routines.

Code: Select all

  ;Structure For Usefull Printer Information
  Structure PInfo
    HDPmm.d     ;Horizontal DPI
    VDPmm.d     ;Vertical DPI
    TopMargin.i
    LeftMargin.i
    BottomMargin.i
    RightMargin.i
    Width.i       ;Actual selected paper width
    Height.i      ;Actual Paper height
    UsableWidth.i
    UsableHeight.i
  EndStructure

  Global PrinterInfo.PInfo

I know some of these can be obtained by using the vector drawing commands but they require me to start printing which opens a document in the print queue which then later has to be cancelled. No CancelPrint procedure.

I have code for the margins which works fine based on the code pointed to by wilbert in another thread. The main things are actual paper width\height and the printer DPI, usable width etc can be inferred from the rest.

Looking a little deeper. If the paper widths etc can be return in mm then there is no need for the printer DPI to be known so can be ignored.

Can anyone show me how to fill in all the above without StartPrinting()?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

I haven't been able to get the dpi.
As for the other things ...

Code: Select all

PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")

CocoaMessage(@TopMargin.CGFloat, PrintInfo, "topMargin")
CocoaMessage(@LeftMargin.CGFloat, PrintInfo, "leftMargin")
CocoaMessage(@BottomMargin.CGFloat, PrintInfo, "bottomMargin")
CocoaMessage(@RightMargin.CGFloat, PrintInfo, "rightMargin")
Debug TopMargin
Debug LeftMargin
Debug BottomMargin
Debug RightMargin

CocoaMessage(@Size.NSSize, PrintInfo, "paperSize")
Debug Size\width
Debug Size\height

CocoaMessage(@Rect.NSRect, PrintInfo, "imageablePageBounds")
;Debug Rect\origin\x
;Debug Rect\origin\y
Debug Rect\size\width
Debug Rect\size\height
The values are in points so you can convert yourself to mm.
1 point = 0.352777778 mm

Code: Select all

point2mm.d = 254 / 720

PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")

CocoaMessage(@Size.NSSize, PrintInfo, "paperSize")
Debug StrD(Size\width * point2mm, 1)
Debug StrD(Size\height * point2mm, 1)
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

Lovely wilbert

Just seems to give some strange results

I modified the code to get mm as you suggested:-

Code: Select all

point2mm.d = 254 / 720

PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")

CocoaMessage(@TopMargin.CGFloat, PrintInfo, "topMargin")
CocoaMessage(@LeftMargin.CGFloat, PrintInfo, "leftMargin")
CocoaMessage(@BottomMargin.CGFloat, PrintInfo, "bottomMargin")
CocoaMessage(@RightMargin.CGFloat, PrintInfo, "rightMargin")
Debug "Top Margin " + StrD(TopMargin * point2mm)
Debug "Left Margin " + Str(LeftMargin * point2mm)
Debug "Bottom Margin " + Str(BottomMargin * point2mm)
Debug "Right Margin " + Str(RightMargin * point2mm)

CocoaMessage(@Size.NSSize, PrintInfo, "paperSize")
Debug "Page Width " + Str(Size\width * point2mm)
Debug "Page Height " + Str(Size\height * point2mm)

CocoaMessage(@Rect.NSRect, PrintInfo, "imageablePageBounds")
;Debug Rect\origin\x
;Debug Rect\origin\y
Debug "Usable Width " + Str(Rect\size\width * point2mm)
Debug "Usable Height " + Str(Rect\size\height * point2mm)
The left and right margins are displayed as 25mm so I thought that the useable page width would be the actual page width minus the left and right margins but this does not seem to be the case. The printer margins are also actually 3mm on all four margins.

I have tried adding PrintRequester and StartPrinting() etc but no change.

It is obvious that I am missing something but where to start looking?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

I think you misunderstand the usable width and height it returns.
It has nothing to do with the set margins.
The area "imageablePageBounds" returns is the area the printer can physically print.
Most printers aren't able to cover the entire paper and this gives you information about it.
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

The printer I am using can print to the whole page area. The printer has default soft margins I have found of 3mm all round but these can be disabled by selecting borderless printing.

When I run similar code on windows, using API calls, it returns everything correctly although I do have to infer the right and bottom margins from the x and y offsets and the printable area.

Strangely however on the MAC the vector drawing functions return the whole page width and height no matter whether borderless is on or off! e.g when A4 is selected the usable width is reported as 210mm whether borderless is on or off but under windows it is reported as 210mm with borderless on and 204 for normal printing.

Perplexed

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

If what you want to know is the page size minus the margins, you can calculate it yourself.

width - leftMargin - rightMargin
height - topMargin - bottomMargin

What kind of values does Windows return ?
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

Hi wilbert
The left and right margins are displayed as 25mm so I thought that the useable page width would be the actual page width minus the left and right margins but this does not seem to be the case.
Tried calculating the page height etc.

What is needed is the actual margins imposed by the printer on any document to be printed. The margins returned seem to be as if a document was being printed. The top margin is reported as almost an inch etc. Windows reports this margin as 3mm and you can print to that margin. With windows you can also select borderless and print right to the edge of the paper on this HP Envy printer. On the MAC opening the PrintRequester and selecting A4 borderless has no effect on the margins reported. Windows also reports the usable pagewidth which is the actual pagewidth minus the left and right margins imposed by the printer. On the values returned by the MAC subtracting the left and right margins from actual page size gives a different width to that reported, similar with the heights.

The MAC seems to be reporting margins on something other than the printer as when borderless no margins should be reported, zero for all, and the available rectangle should be the same as the printable area.

I have been reading, not the best, but does an NSView get involved somewhere? Bear in mind I haven't got a clue what an NSView is! It just seems to be a document that the MAC is attempting to print and it maybe the margins on this that are being reported.

Just to be a little clearer both Windows and MAC are trying to print to the same printer on the same wifi network and I have downloded and installed the latest printer driver for the MAC.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

How about the values the code below outputs.
Are those more what you expect ?

Code: Select all

ImportC ""
  PMGetPageFormatPaper (format, *paper)
  PMPaperGetMargins (paper, *paperMargins)  
EndImport

Structure PMRect
  top.d
  left.d
  bottom.d
  right.d
EndStructure

PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")

PageFormat = CocoaMessage(0, PrintInfo, "PMPageFormat")
If PMGetPageFormatPaper(PageFormat, @Paper) = 0 
  If PMPaperGetMargins(Paper, @Margins.PMRect) = 0
    Debug Margins\top
    Debug Margins\left
    Debug Margins\bottom
    Debug Margins\right
  EndIf
EndIf

; CocoaMessage(@TopMargin.CGFloat, PrintInfo, "topMargin")
; CocoaMessage(@LeftMargin.CGFloat, PrintInfo, "leftMargin")
; CocoaMessage(@BottomMargin.CGFloat, PrintInfo, "bottomMargin")
; CocoaMessage(@RightMargin.CGFloat, PrintInfo, "rightMargin")
; Debug TopMargin
; Debug LeftMargin
; Debug BottomMargin
; Debug RightMargin

CocoaMessage(@Size.NSSize, PrintInfo, "paperSize")
Debug Size\width
Debug Size\height

CocoaMessage(@Rect.NSRect, PrintInfo, "imageablePageBounds")
; Debug Rect\origin\x
; Debug Rect\origin\y
Debug Rect\size\width
Debug Rect\size\height
This might also work

Code: Select all

Structure PMRect
  top.d
  left.d
  bottom.d
  right.d
EndStructure

ImportC ""
  PMCreateSession (*printSession)
  PMCreatePageFormat (*pageFormat)
  PMSessionDefaultPageFormat (printSession, pageFormat)
  PMGetUnadjustedPageRect (pageFormat, *pageRect.PMRect)
  PMGetUnadjustedPaperRect (pageFormat, *paperRect.PMRect)
  PMRelease (object)
EndImport

If PMCreateSession(@Session) = 0
  If PMCreatePageFormat(@PageFormat) = 0
    PMSessionDefaultPageFormat(Session, PageFormat)
    PMGetUnadjustedPaperRect(PageFormat, @PaperRect.PMRect)
    PMGetUnadjustedPageRect(PageFormat, @PageRect.PMRect)
    Debug -PaperRect\top                      ; top margin
    Debug -PaperRect\left                     ; left margin
    Debug PaperRect\bottom - PageRect\bottom  ; bottom margin
    Debug PaperRect\right - PageRect\right    ; right margin
    Debug PaperRect\right - PaperRect\left    ; paper width
    Debug PaperRect\bottom - PaperRect\top    ; paper height
    Debug PageRect\right                      ; printable width
    Debug PageRect\bottom                     ; printable height
    PMRelease(PageFormat)
  EndIf
  PMRelease(Session)
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

Thanks again,

These certainly get closer to the real values but the margins are still reported twice the size. The margins are also still reported when borderless is selected. They should all be zero at that point.

It is something in OSX. In order to move my programme along I inserted the correct values as tested on windows and attempted to print but got strange results from the MAC. The printing cutting off at strange points. It is not the printer the OS seems to have decided what margins I shall have and is clipping the printer output.

Printing on the MAC seems to be quite weird.

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

If it helps I have now published my code where I am attempting to print here

http://www.purebasic.fr/english/viewtop ... 12&t=65192
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

collectordave wrote:If it helps I have now published my code where I am attempting to print here

http://www.purebasic.fr/english/viewtop ... 12&t=65192
I don't know exactly how to help at the moment.
If I select a different paper size for my printer, margins are also changing and if it's a paper type with no defined margins they are reported as 0.
Maybe you should take a look at the Core Printing Reference if you haven't already and see if there's information there that could help you out.
https://developer.apple.com/library/mac ... ePrintRef/
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

Hi

Selecting a different paper size should not change the margins as the margins i am trying to get are the margins imposed by the printer and nothing more i.e. the unprintable parts of the page for any particulr printer.

I have since tried Pages on the MAC and get weird results there as well whether I select A4 or A4 borderless in the page setup the page setup does not change I am still left with margins on the document which I cannot simply change.

I have also tried printing with PB and something cuts off the print well before the edge of the page this is not the printer as in windows it prints correctly.

It seems that Apple cannot handle printers correctly.

I will have a look at the core printing reference. Thanks for the link. Maybe the
PMPrinter for information about a printer
will hold the printer information that I need?

Thanks again
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing

Post by wilbert »

collectordave wrote:It seems that Apple cannot handle printers correctly.
Apple is used by a lot of professional designers so that will probably not be the case :)
But it's very well possible OSX behaves differently from what you are used to from Windows.

One option might be to create a borderless paper type.
If you have the print dialog and look at the combobox to select the paper size, you see at the bottom of this list the option to create a custom one.
There you could create a custom paper type with all margins set to 0.
This custom paper dialog however can detect the printer margins so it should be possible to get them. :?

Edit:
It looks like the hardware information you want might be HWMargins defined in the PPD file.
Unfortunately I don't know how to get to that PPD file for the selected printer.
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Printing

Post by collectordave »

Ok

using the code below I am simply trying to print a 1mm thick box 10mm in from the edge of the paper.

Code: Select all


Global HDPmm.d,VDPmm.d
Global PWidth,PHeight,PUsableWidth,PUsableHeight,PTopMargin,PLeftMargin,PBottomMargin,PRightMargin



;Change these for the paper size in your printer
;These are for A4
Define PageWidth.i = 210
Define PageHeight.i = 297

  Procedure GetPrinterInfo()
        
      printer_DC.l = StartDrawing(PrinterOutput())

      If printer_DC
        HDPmm = GetDeviceCaps_(printer_DC,#LOGPIXELSX) / 25.4
        VDPmm = GetDeviceCaps_(printer_DC,#LOGPIXELSY) / 25.4
        PWidth = GetDeviceCaps_(printer_DC,#PHYSICALWIDTH) / HDPmm
        PHeight = GetDeviceCaps_(printer_DC,#PHYSICALHEIGHT) / VDPmm
        PUsableWidth = GetDeviceCaps_(printer_DC,#HORZSIZE) ; Returns mm
        PUsableHeight = GetDeviceCaps_(printer_DC,#VERTSIZE); Returns mm
        PTopMargin = GetDeviceCaps_(printer_DC,#PHYSICALOFFSETY) / VDPmm
        PLeftMargin = GetDeviceCaps_(printer_DC,#PHYSICALOFFSETX) / HDPmm
        PBottomMargin = PHeight - PUsableHeight - PTopMargin
        PRightMargin = PWidth - PUsableWidth - PLeftMargin
      EndIf

      StopDrawing()
   
EndProcedure



If PrintRequester()
 
  StartPrinting("Test")
  
  GetPrinterInfo()
  
  StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
  Debug "HDPI " + Str(HDPmm)
  Debug "VDPI " + Str(VDPmm)
  Debug "Vector W " + Str(VectorOutputWidth())  
  Debug "Vector H " + Str(VectorOutputHeight())
  Debug "Page Width " + Str(PWidth)
  Debug "Page Height " + Str(PHeight)
  Debug "Usable Width " + Str(PUsableWidth)
  Debug "Usable Height " + Str(PUsableHeight)
  Debug "Top Margin " + Str(PTopMargin)
  Debug "Left Margin " + Str(PLeftMargin)
  Debug "Bottom Margin " + Str(PBottomMargin)
  Debug "Right Margin " + Str(PRightMargin)
  
  ;Draw a box 10mm from the edges of the page
  AddPathBox(10 - PLeftMargin, 10 - PTopMargin, PageWidth - 20, PageHeight - 20)
  StrokePath(1)
  
  StopVectorDrawing()
 
  StopPrinting()
 
EndIf
Running this on Windows I get a 1mm thick box 10mm from the edges whether I select borderless or not

Running this on the MAC without selecting borderless I get two sides of the box the left and right both 10mm from the edge of the paper. The Top and Bottom of the box is missing.

Selecting the borderless paper type I get a smaller box, not 10mm from each edge, but it is all drawn.

Creating my own paper type with no borders\margins gives me the same as if I did not select borderless.

However when creating my own paper type I selected my printer and the minimum margins for the top and bottom are shown as 12.7mm? The left and right are shown as 3.18mm which is very close. This would explain why the Top and bottom are cut off

These margins are wrong for this printer it imposes a 3mm margin all round so to me it seems like a bug in the driver for the printer on the MAC as these are gathered from the driver somehow I assume.

I was then getting curious so I changed the box to print 15mm from each edge and it worked. A nice box 15mm from each edge.

Need to do some more testing but from that it maybe that I am wrong about the MAC and wrong about the bug report I have put in for printing on the MAC. It seems PB tells you the entire printable area regardless.

If right then we do not need to do anything to get printer margins etc except maybe to warn users that set margins very small that they are less than the printer margins I only need the Windows offsets.

Thanks for your help wilbert it got me going in the right direction I will try my printing lib on the MAC now.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Printing

Post by fsw »

Started to write some code for printing.

@collectordave

This

Code: Select all

AddPathBox(10, 10, PageWidth - 20, PageHeight - 20)
gives the expected result.

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply