Page 1 of 1

Best way to print recipes

Posted: Fri Feb 26, 2016 12:01 pm
by Fangbeast
I've just about got 90% of my recipe program going thanks to InfraTec's writing of the import code, he was absolutely terrific.

And Kenmo's rating gadget make a great addition too.

But now I am turning my thoughts to printing and i realise that my normal method of 'filling in' a web template isn't good enough to get page and size controls to cater to the printer.

Ideally, I'd like to be able to print a recipe as I see them in magazines, mostly the photos in the top left, ingredients to the right and the instructions underneath it all.

What would be the best way of doing this?

Continue to use my web template with absolutely no control over the printing, pages etc, xml dialog for page control layout of the vector lib?

Does anyone have any ideas and some sample code to teach me what I should be doing?

I can beg if it helps and also lend you srod's abused goat collection!

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 12:33 pm
by mhs
The Vector Drawing lib is your friend :)

I just unfortunately no sample code at hand, but to make it works well.

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 1:42 pm
by Fangbeast
mhs wrote:The Vector Drawing lib is your friend :)

I just unfortunately no sample code at hand, but to make it works well.
I understand your point but understand mine too, I am not good with graphics or maths so I need some concrete examples to follow or I just don't get it.

The web template way I do things is not good for this sort of thing, absolutely no control unless you know html off the top of your head.

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 1:52 pm
by IdeasVacuum
Well essentially there are only the two most popular paper sizes to support, ANSI A (aka Letter) and ISO A4. ANSI A is 215.9 x 279.4 mm and ISO A4 is 210 x 297mm, so you can define a 'hybrid' margin (border) size to support both in 1 layout that will print on either size sheet.

So, I would consider using a Container to represent the paper, arrange an image gadget and two Editor gadgets in the layout you described for on-screen viewing and then translate this to printable output. You could indeed output a PDF, which is both shareable and printable - the print quality delivered by PDF + Adobe Reader is pretty good.

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 9:57 pm
by Fangbeast
IdeasVacuum wrote: So, I would consider using a Container to represent the paper, arrange an image gadget and two Editor gadgets in the layout you described for on-screen viewing and then translate this to printable output. You could indeed output a PDF, which is both shareable and printable - the print quality delibered by PDF + Adobe Reader is pretty good.
i looked at the pdf stuff once, too complicated for my brain. But as I said above "so I need some concrete examples to follow or I just don't get it."

:):)

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 11:23 pm
by TI-994A
Fangbeast wrote:...I need some concrete examples to follow or I just don't get it.
Here's something I put together just for you!

Code: Select all

Procedure PrintRecipe(recipeName.s, image.i, ingredients.s, method.s)
  
  recipeTitleFont = LoadFont(#PB_Any, "Arial", 24, #PB_Font_Bold)
  recipeBodyFonts = LoadFont(#PB_Any, "Times New Roman", 14)
  recipeHeaderFonts = LoadFont(#PB_Any, "Times New Roman", 22, 
                               #PB_Font_Bold | #PB_Font_Italic)
  If PrintRequester()
    If StartPrinting("Fangbeast's Recipe Printer")  
      If StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
        
        frameWidth = 1
        singleSpace = 5
        doubleSpace = 10
        pageMargin = 10
        pageWidth = VectorOutputWidth()
        pageHeight = VectorOutputHeight()
        
        imgAspect.d = ImageWidth(image) / ImageHeight(image)
        imgWidth.d = (pageWidth - (pageMargin * 2)) / 2
        imgHeight.d = imgWidth * imgAspect
        
        MovePathCursor(pageMargin, pageMargin)
        AddPathLine(pageWidth - (pageMargin * 2), 0, #PB_Path_Relative)
        AddPathLine(0, pageHeight - (pageMargin * 2), #PB_Path_Relative)
        AddPathLine(-(pageWidth - (pageMargin * 2)), 0, #PB_Path_Relative)
        AddPathLine(0, -(pageHeight - (pageMargin * 2)), #PB_Path_Relative)
        StrokePath(frameWidth, #PB_Path_SquareEnd)
        
        VectorFont(FontID(recipeTitleFont))
        VectorSourceColor(RGBA(0, 0, 155, 255))
        titleXMargin = (pageWidth - VectorTextWidth(recipeName)) / 2
        titleYMargin = pageMargin + singleSpace
        MovePathCursor(titleXMargin, titleYMargin)
        DrawVectorText(recipeName)
        
        startX = pageMargin + singleSpace
        startY = PathCursorY() + (singleSpace * 3)
        MovePathCursor(startX, startY)
        DrawVectorImage(ImageID(image), 255, imgWidth, imgHeight)
        imageBase = PathCursorY()
        
        startX = PathCursorX() + singleSpace
        VectorFont(FontID(recipeHeaderFonts))
        VectorSourceColor(RGBA(0, 0, 0, 255))
        MovePathCursor(startX, startY)
        DrawVectorText("Ingredients")
        
        startY = PathCursorY() + doubleSpace
        VectorFont(FontID(recipeBodyFonts))
        MovePathCursor(startX, startY)
        paragraphWidth = pageWidth - (startX + pageMargin + singleSpace)
        DrawVectorParagraph(ingredients, paragraphWidth, 150)
        
        startX = pageMargin + singleSpace
        startY = imageBase + doubleSpace
        VectorFont(FontID(recipeHeaderFonts))
        VectorSourceColor(RGBA(0, 0, 0, 255))
        MovePathCursor(startX, startY)
        DrawVectorText("Method")
        
        startY = PathCursorY() + doubleSpace
        VectorFont(FontID(recipeBodyFonts))
        MovePathCursor(startX, startY)
        paragraphWidth = pageWidth - ((pageMargin * 2) + doubleSpace)
        paragraphHeight = pageHeight - (startY + pageMargin)
        DrawVectorParagraph(method, paragraphWidth, paragraphHeight)
        
        StopVectorDrawing()
      EndIf
    EndIf
    StopPrinting()
  EndIf
  
EndProcedure


;demo for downloading sample recipe files from DropBox (requires PureBasic v5.4+)

UseJPEGImageDecoder()
InitNetwork()

Define.s ingredients, method, recipeName = "Mini Strawberry Cheesecakes"

ReceiveHTTPFile("https://www.dropbox.com/s/sd0xaeqybtehb1x/cheeseCake.jpg?dl=1",
                GetTemporaryDirectory() + "cheeseCake.jpg")
image = LoadImage(#PB_Any, GetTemporaryDirectory() + "cheeseCake.jpg")

ReceiveHTTPFile("https://www.dropbox.com/s/m2hnogqhab2b588/ingredients.txt?dl=1",
                GetTemporaryDirectory() + "ingredients.txt")
ReadFile(0, GetTemporaryDirectory() + "ingredients.txt")
While Not Eof(0)
  ingredients + ReadString(0) + #CRLF$
Wend
CloseFile(0)

ReceiveHTTPFile("https://www.dropbox.com/s/lh517igi6bddtgh/method.txt?dl=1",
                GetTemporaryDirectory() + "method.txt")
ReadFile(0, GetTemporaryDirectory() + "method.txt")
While Not Eof(0)
  method + ReadString(0) + #CRLF$
Wend
CloseFile(0)

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, #PB_Any, #PB_Any, 600, 300, "Recipe Printer", wFlags)
TextGadget(#PB_Any, 10, 10, 180, 20, "Raw ingredients text:", #PB_Text_Center)
TextGadget(#PB_Any, 200, 10, 180, 20, "Raw method text:", #PB_Text_Center)
TextGadget(#PB_Any, 400, 10, 180, 20, "Recipe image:", #PB_Text_Center)
SetGadgetText(EditorGadget(#PB_Any, 10, 30, 180, 180, #PB_Editor_WordWrap), ingredients)
SetGadgetText(EditorGadget(#PB_Any, 200, 30, 180, 180, #PB_Editor_WordWrap), method)
CopyImage(image, image2)
ResizeImage(image2, 180, 180)
ImageGadget(#PB_Any, 400, 30, 180, 180, ImageID(image2))
printButton = ButtonGadget(#PB_Any, 10, 250, 580, 40, "PRINT SAMPLE RECIPE")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case printButton
          PrintRecipe(recipeName, image, ingredients, method)           
      EndSelect
  EndSelect
Until appQuit = 1
And here's a sample printout:

Image

All you need is the PrintRecipe() procedure, passing it the recipe name, ingredients and method texts, and an image. The rest of it is simply for the demonstration, downloading the sample files, display the raw data, etc.

Not perfect, but hope you'd find it useful. :D
EDITS wrote:18th February 2019: updated download links

Re: Best way to print recipes

Posted: Fri Feb 26, 2016 11:50 pm
by juror
Impressive :shock:

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 12:47 am
by IdeasVacuum
very 8)

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 3:12 am
by Fangbeast
That's beautiful. If my tiny pig brain caused by years of abusing srod's deformed sheep can understand it, I will be in pig heaven!!

That example is very sminky.

I can't wait to finish the million other things I have to do to this program to release it, it will look like magic.

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 8:08 am
by davido
@TI-994A,
Beautiful.
An impressive demonstration of printing with vector drawing library. 8)
Thank you for sharing.

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 10:32 am
by TI-994A
Thank you for all the kind words. I'm truly flattered, and I'm truly grateful. :D

Most of all, I'm so glad that you've found it good enough to include in your project, Fangbeast. Thank you.

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 11:21 am
by Fangbeast
TI-994A wrote:Thank you for all the kind words. I'm truly flattered, and I'm truly grateful. :D

Most of all, I'm so glad that you've found it good enough to include in your project, Fangbeast. Thank you.
Are you kidding me?? My tired brain couldn't code its way out of a diseased sheep's testicles to think of things like that so I put it in because it looks great and it works!! (I blame that srod for the diseased sheep).

Everything I do that I can't handle (and there is a lot!!) I yell loudly for to get help and nearly everything I do is a collaborative effort.

There are so many nice bits in this recipe manager now, hope that I can make it even better and give back to everyone for all the years of help.

My claim to fame is being a good assembler of bits and bobs, a trained monkey:):):)

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 11:37 am
by srod
That is very nice TI-994A. When did the HTTP lib start supporting HTTPS? :shock:

Fangles, those goats and sheep were in pristine condition when you took delivery of them. They still had the wrapping on them for Pete's sake! Poor old Flossie was so traumatised on her return that she threw herself into the Lion's den at London zoo... but the Lion's threw her back as there is so little meat left on her!

I'd ask what you did to them, but then I'd likely be so traumatised that I would have to throw myself into the Lion's den as well!

Stick to abusing post boxes will you and leave the poor animals alone!

Re: Best way to print recipes

Posted: Sat Feb 27, 2016 3:59 pm
by TI-994A
srod wrote:That is very nice TI-994A. When did the HTTP lib start supporting HTTPS? :shock:
Thanks srod. Not sure on that one; all I know is that PureBasic's ReceiveHTTPFile() function stopped working with DropBox files one or two versions prior to 5.4, following some DropBox protocol change (please see this thread). However, even then, it appears to have worked on the HTTPS protocol.