Linux的/ CUPS打印例子/教程? [英] Linux/CUPS printing example/tutorial?

查看:1337
本文介绍了Linux的/ CUPS打印例子/教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印一些矢量数据(或者更精确地​​说:一些点和多义线)使用Linux。当我问谷歌为它显示了我很多的教程和文档 - 但所有的最终用户,无需编程实例

I need to print some vector data (or to be more exact: some dots and polylines) using Linux. When I ask Google for that it shows me many tutorials and documentations - but all for end users, no programming examples.

因此​​,没有任何人知道一个优秀的程序员HOWTO /教程,展示Linux下的打印?

So does anybody know a good programmers HOWTO/tutorial that shows printing under Linux?

谢谢!

推荐答案

CUPS没有它自己的文档描述API。它并不需要之一:格式,如的PostScript,PDF和JPEG是CUPS的一等公民。无论您使用的程序或API,你想创建一个这样的文件,并且你然后将其发送到CUPS(与 LPR 或使用CUPS API)和杯子将它转换为适当的内部格式,并将其发送到打印机。

CUPS doesn't have its own document description API. It doesn't need one: formats like PostScript, PDF, and JPEG are first-class citizens of CUPS. You use whatever program or API you like to create a such a file, and you then send it to CUPS (with lpr or with the CUPS API) and CUPS will transform it to the appropriate internal format and send it to the printer.

因此​​,对于你的情况,你可能使用矢量图形库如开罗创作的PostScript,然后您发送关闭到CUPS打印。这里有一个简单的C例如:

So, for your case, you might use a vector graphics library like Cairo to author PostScript, and then you send that off to CUPS for printing. Here's a simple C example:

// compile with:
//   gcc -Wall -o cairo_print cairo_print.c `pkg-config --cflags --libs cairo` `cups-config --cflags --libs`

#include <stdio.h>
#include <cairo.h>
#include <cairo-ps.h>
#include <cups/cups.h>

// A4 width, height in points, from GhostView manual:
//   http://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.html
#define WIDTH  595  
#define HEIGHT 842  

int main(int argc, char** argv) {
  if (argc!= 2){
    fprintf (stderr, "usage: %s word\n", argv[0]);
    return 1;
  }

  // setup
  char* tmpfilename = tempnam(NULL,NULL);
  cairo_surface_t* surface = cairo_ps_surface_create(tmpfilename, 
                                                     WIDTH, 
                                                     HEIGHT);
  cairo_t *context = cairo_create(surface);

  // draw some text
  cairo_select_font_face(context, 
                         "Arial Black", 
                         CAIRO_FONT_SLANT_NORMAL, 
                         CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size(context, 30);
  cairo_move_to(context, WIDTH/2, HEIGHT/2);
  cairo_show_text(context, argv[1]); // the text we got as a parameter

  // draw a dotted box
  const double pattern[] = {15.0, 10.0};
  cairo_set_dash(context, pattern, 2, 0);
  cairo_set_line_width(context, 5);
  cairo_rectangle(context, WIDTH*0.33, HEIGHT*0.33, WIDTH*0.5, WIDTH*0.5);
  cairo_stroke(context);

  // finish up
  cairo_show_page(context);
  cairo_destroy(context);
  cairo_surface_flush(surface);
  cairo_surface_destroy(surface);

  // print
  cupsPrintFile(cupsGetDefault(), tmpfilename, "cairo PS", 0, NULL);
  unlink(tmpfilename);

  return 0;
}

严格,你不需要那么临时文件:CUPS的API,可以构建一个流,你可以发出的页面数据转换成(但文件是非常方便的调试)

Strictly you don't need that temporary file: the CUPS API allows you to construct a stream and you can emit the page data into that (but a file is handy for debugging).

这篇关于Linux的/ CUPS打印例子/教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆