Advice on Error Message - Invalid Graphics Mode

258 views
Skip to first unread message

Klaus

unread,
Nov 29, 2008, 12:15:25 AM11/29/08
to libHaru
I'm using PHP's interface to access the Haru library...

http://us3.php.net/manual/en/book.haru.php

I keep getting an "Invalid Graphics Mode" during testing when I switch
from outputting lines and curves to outputting text. I build a sample
manually and now error gets thrown. As soon as I try to put something
a bit more complex and organized together by using functions and
objects, I run into this wall.

Here's the real-life example to make this more interesting...

I'm building an application that will build plans according to spec,
basically a lot of line drawing.

So far so good.

Then I started work on an object to create a set of braces in an
alternate color and pattern, to indicate measurement. The braces
SHOULD have a text label at the pointy end with a number and unit of
measure.

When I build this on one page by tweaking one of the samples available
online, no problem. As soon as I move code into functions (haven't
event touched objects yet), I run into this roadblock of libHaru
throwing "Invalid Graphics Mode" errors.

Hopefully it's just some oversite on my part and not an issue with the
PHP interface.

Thanks,
Klaus

Antony Dovgal

unread,
Nov 29, 2008, 4:12:52 PM11/29/08
to lib...@googlegroups.com
On 29.11.2008 08:15, Klaus wrote:
> I'm using PHP's interface to access the Haru library...
>
> http://us3.php.net/manual/en/book.haru.php
>
> I keep getting an "Invalid Graphics Mode" during testing when I switch
> from outputting lines and curves to outputting text. I build a sample
> manually and now error gets thrown. As soon as I try to put something
> a bit more complex and organized together by using functions and
> objects, I run into this wall.

> Hopefully it's just some oversite on my part and not an issue with the
> PHP interface.

I need something more informative than just an error message and a description of your actions.
A short reproduce script would be best.

--
Wbr,
Antony Dovgal

David Alberto

unread,
Jun 16, 2014, 9:44:43 AM6/16/14
to lib...@googlegroups.com
I'm having a similar issue to the one Klaus had. I'm trying to create a pdf that has a logo image and some information below. The code below raises a HaruException. I just started working with the Haru library and I'm fairly new to PHP overall so I have no idea what this means or how to fix it.
$doc = new HaruDoc();
$page = $doc->addpage();
$page -> setSize(HaruPage::SIZE_LEGAL, HaruPage::PORTRAIT);
$page->setTextRenderingMode(HaruPage::FILL_THEN_STROKE); /* fill and stroke text */


//Instantiate HaruImage object
//Add image to pdf
$logoImage = $doc->loadJPEG('C:\wamp\www\public_html\logos\\'.$dealerID.'_logo.jpg');
$imageSize = $logoImage->getSize();
$imageWidth = $imageSize['width'] * 0.2;
$imageHeight = $imageSize['height'] * 0.2;
$page->drawImage($logoImage, 81, 932, $imageWidth, $imageHeight);

//Add text
$courier = $doc->getFont("Courier-Bold");
$page->setFontAndSize($courier, 9.5);
$page->TextRect(61, 922, $imageWidth + 20, 850, $info, HaruPage::TALIGN_CENTER);

Bill Horger

unread,
Jun 16, 2014, 10:20:31 AM6/16/14
to lib...@googlegroups.com
The one thing I don't see is a generic test for what mode you are actually in. I don't use text rectangles, but I suspect that this may be the issue.

In my own library, I include these kinds of tests (C++) when I am getting ready to use a specific mode. I've highlighted the most important ones as BOLD.

Here, I'm getting ready to output a series of text lines:
    Page_Status = HPDF_Page_GetGMode(page);   
    // close out the line being drawn
    if(Page_Status == HPDF_GMODE_PATH_OBJECT)    HPDF_Page_Stroke (page);

    HPDF_Page_BeginText (page);

    HPDF_Page_SetFontAndSize (page, current_font, (HPDF_REAL)PDFGraph_ConvertUnits(height));
    HPDF_Page_SetRGBStroke(page, PDFGraph_EXTRACT_RED(TextColor), PDFGraph_EXTRACT_GREEN(TextColor),PDFGraph_EXTRACT_BLUE(TextColor));
    HPDF_Page_SetRGBFill(page,PDFGraph_EXTRACT_RED(TextColor), PDFGraph_EXTRACT_GREEN(TextColor),PDFGraph_EXTRACT_BLUE(TextColor));

Here, I'm getting ready to output a set of line drawing commands or shape drawing (like arcs):
    Page_Status = HPDF_Page_GetGMode(page);   
    if(Page_Status == HPDF_GMODE_PAGE_DESCRIPTION || Page_Status == HPDF_GMODE_TEXT_OBJECT)
    {
       HPDF_Page_SetRGBStroke(page,PDFGraph_EXTRACT_RED(LineColor),PDFGraph_EXTRACT_GREEN(LineColor),PDFGraph_EXTRACT_BLUE(LineColor));
       HPDF_Page_SetLineWidth (page,(HPDF_REAL)PDFLineWidth) != HPDF_OK)
// This function is required whenever the mode has changed
HPDF_Page_MoveTo(page,(HPDF_REAL)newx[0],(HPDF_REAL)newy[0]);
    }

For line drawing (using multiple LINETO calls, I close out the line with this function as I verify the status:
HPDF_Page_Stroke (page);

Sorry the formatting does not come through on e-mail very well! Hope this helps.
Bill



--
--
---
libHaru.org development mailing list
To unsubscribe, send email to libharu-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "libHaru" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libharu+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Darrell Ross

unread,
Nov 17, 2014, 6:42:10 PM11/17/14
to lib...@googlegroups.com
You need to manage your GMODE. I can't seem to find the new help version of this page but it is has a helpful image: http://libharu.sourceforge.net/graphics.html#Graphics_mode_.

When you call a function like drawImage, it will set the GMODE to HPDF_GMODE_PATH_OBJECT. Prior to doing any text, you must set the GMODE to HPDF_GMODE_TEXT_OBJECT. You can do this very simply with the calls:
  • HPDF_Page_BeginText() 
  • HPDF_Page_EndText().
You will get fewer errors if you wrap all of your text output routines within these.

Similar to Bill, I also check for GMODE before trying to do stuff though with a bit of a different structure.

ErrorEnum PDFDocument::SetFontAndSize()
{
  HPDF_Page page
= HPDF_GetCurrentPage(PDF);
  if(!page)
   
return ERROR_PAGE_NOT_FOUND;

 
if(HPDF_Page_GetGMode(page) != HPDF_GMODE_TEXT_OBJECT)
   
return ERROR_INVALID_GMODE;

  HPDF_Font fontFace
= GetFont();
  HPDF_Page_SetFontAndSize
(page, fontFace, fontSizeDefault);
 
return ERROR_NONE;
}


Reply all
Reply to author
Forward
0 new messages