2014-02-24 2 views
0

나는 pycairo를 사용하여 pdf 보고서를 만들 수있었습니다. 표지는 괜찮 았지만 두 번째 페이지는 다이어그램의 크기에 따라 방향을 변경해야합니다. pycairo을 사용하여 한 페이지 가로 및 세로 페이지를 어떻게 설정합니까? 고맙습니다. pycairo를 사용하여 pdf 파일의 가로 방향 설정

여기서 두 번째 페이지를 생성하는 제 함수이다

def draw_pedigree(ctx, path_to_image, left, top, container_width, container_height): 
# create a new page to draw a pedigree 
ctx.show_page() 
ctx.save() 

# load image from input path 
image_surface = cairo.ImageSurface.create_from_png(path_to_image) 

img_height = image_surface.get_height() 
img_width = image_surface.get_width() 

print "img_height: %d" % img_height 
print "img_width: %d" % img_width 
print "container_height: %d" % container_height 
print "container_width: %d" % container_width 

if (img_width <= container_width and img_height <= container_height): # this case the image don't need to be scaled or rotated 
    ctx.translate(left, top) 
    ctx.set_source_surface(image_surface) 
    ctx.paint() 
    ctx.restore() 
else: # this case the image need to be scaled 

    # check should image be rotated or not 
    if(float(img_height)/float(img_width) <= 0.875): # rotate the image 
     width_ratio = float(container_height)/float(img_width) 
     height_ratio = float(container_width)/float(img_height) 
     scale_xy = min(height_ratio, width_ratio) 
     print "scale_xy: %f" % scale_xy 

     ctx.translate(545, top) 
     ctx.rotate(math.pi/2) 

     if(scale_xy < 1): 
      ctx.scale(scale_xy, scale_xy) 

     ctx.set_source_surface(image_surface) 
     ctx.paint() 
     ctx.restore() 
    else: # don't rotate the image 
     # calculate proportional scaling 
     width_ratio = float(container_width)/float(img_width) 
     height_ratio = float(container_height)/float(img_height) 
     scale_xy = min(height_ratio, width_ratio) 
     ctx.scale(scale_xy, scale_xy) 
     ctx.translate(left, top) 
     ctx.set_source_surface(image_surface) 
     ctx.paint() 
     ctx.restore() 

enter image description here

+0

지금까지 나는 항상 페이지에 맞게 그 폭이 너무 긴 경우를 회전 할 수 있도록 다이어그램을 확장하기 위해 노력했다. –

답변

1

PDF로 표면이 set_size 방법이있다.

예를 들어

# Set A4 portrait page size 
surface.set_size(595, 842) 

# Set A4 landscape page size 
surface.set_size(842, 595) 
+0

의견을 보내 주셔서 감사합니다. 나는 이미 이것을 시도했지만 여러 페이지에서 작동하지 않습니다. –

+0

페이지에서 그림을 그리기 전에 set_size를 호출해야합니다. set_size에 버그가있는 이전 버전의 카이로 (1.10.0)가있었습니다. 이것은 1.10.2에서 수정되었습니다. – Adrian

+0

PDF 서페이스를 만들 때 첫 페이지에 대해 set_size (595,842)를 작성합니다. context.show_page()를 호출하여 두 번째 페이지를 만듭니다. 두 번째 페이지에만 어떻게 set_size (842, 595) 할 수 있습니까? 전에 set_site 다시 두 모든 페이지가 변경됩니다 전에 이것을 시도했다. –