📄 pdf.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Uwe Steinmann <Uwe.Steinmann@fernuni-hagen.de> | | Rainer Schaaf <rjs@pdflib.com> | +----------------------------------------------------------------------+*//* $Id: pdf.c,v 1.112.2.11.2.2 2007/01/01 09:46:46 sebastian Exp $ *//* pdflib 2.02 ... 3.0x is subject to the ALADDIN FREE PUBLIC LICENSE. Copyright (C) 1997-1999 Thomas Merz. 2000-2001 PDFlib GmbH *//* Note that there is no code from the pdflib package in this file *//* {{{ includes */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "php.h"#include "php_ini.h"#include "php_globals.h"#include "zend_list.h"#include "ext/standard/head.h"#include "ext/standard/info.h"#include "ext/standard/file.h"#include "php_streams.h"#if HAVE_LIBGD13#include "ext/gd/php_gd.h"#if HAVE_GD_BUNDLED#include "ext/gd/libgd/gd.h"#else#include "gd.h"#endifstatic int le_gd;#endif#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef PHP_WIN32# include <io.h># include <fcntl.h>#endif/* }}} */#if HAVE_PDFLIB#include "php_pdf.h"static int le_pdf;/* {{{ constants * to adopt the php way of error handling to PDFlib * The image related functions in PDFlib return -1 on error * but they may return 0 (FALSE) in normal cases * so this offset will repair this */#define PDFLIB_IMAGE_OFFSET 1#define PDFLIB_FONT_OFFSET 1#define PDFLIB_PDI_OFFSET 1#define PDFLIB_PATTERN_OFFSET 1#define PDFLIB_SPOT_OFFSET 1/* }}} *//* {{{ pdf_functions[] */function_entry pdf_functions[] = { PHP_FE(pdf_new, NULL) /* new function */ PHP_FE(pdf_delete, NULL) /* new function */ PHP_FE(pdf_open_file, NULL) /* new function */ PHP_FE(pdf_get_buffer, NULL) /* new function */ PHP_FE(pdf_close, NULL) PHP_FE(pdf_begin_page, NULL) PHP_FE(pdf_end_page, NULL) PHP_FE(pdf_get_majorversion, NULL) PHP_FE(pdf_get_minorversion, NULL) PHP_FE(pdf_get_value, NULL) PHP_FE(pdf_set_value, NULL) PHP_FE(pdf_get_parameter, NULL) PHP_FE(pdf_set_parameter, NULL) PHP_FE(pdf_findfont, NULL) /* new function */ PHP_FE(pdf_setfont, NULL) /* new function */ PHP_FE(pdf_show, NULL) PHP_FE(pdf_show_xy, NULL) PHP_FE(pdf_continue_text, NULL) PHP_FE(pdf_show_boxed, NULL) PHP_FE(pdf_stringwidth, NULL) /* new parameters: [int font, float size] */ PHP_FE(pdf_set_text_pos, NULL) PHP_FE(pdf_setdash, NULL) PHP_FE(pdf_setpolydash, NULL) /* new function: not yet finished */ PHP_FE(pdf_setflat, NULL) PHP_FE(pdf_setlinejoin, NULL) PHP_FE(pdf_setlinecap, NULL) PHP_FE(pdf_setmiterlimit, NULL) PHP_FE(pdf_setlinewidth, NULL) PHP_FE(pdf_save, NULL) PHP_FE(pdf_restore, NULL) PHP_FE(pdf_translate, NULL) PHP_FE(pdf_scale, NULL) PHP_FE(pdf_rotate, NULL) PHP_FE(pdf_skew, NULL) PHP_FE(pdf_concat, NULL) /* new function */ PHP_FE(pdf_moveto, NULL) PHP_FE(pdf_lineto, NULL) PHP_FE(pdf_curveto, NULL) PHP_FE(pdf_circle, NULL) PHP_FE(pdf_arc, NULL) PHP_FE(pdf_rect, NULL) PHP_FE(pdf_closepath, NULL) PHP_FE(pdf_stroke, NULL) PHP_FE(pdf_closepath_stroke, NULL) PHP_FE(pdf_fill, NULL) PHP_FE(pdf_fill_stroke, NULL) PHP_FE(pdf_closepath_fill_stroke, NULL) PHP_FE(pdf_clip, NULL) PHP_FE(pdf_endpath, NULL) PHP_FE(pdf_open_image_file, NULL) /* new parameters: [char *stringpram, int intparam] */ PHP_FE(pdf_open_ccitt, NULL) /* new function */ PHP_FE(pdf_open_image, NULL) /* new function */ PHP_FE(pdf_close_image, NULL) PHP_FE(pdf_place_image, NULL) PHP_FE(pdf_add_bookmark, NULL) PHP_FE(pdf_set_info, NULL) PHP_FE(pdf_attach_file, NULL) /* new function */ PHP_FE(pdf_add_note, NULL) /* new function */ PHP_FE(pdf_add_pdflink, NULL) PHP_FE(pdf_add_locallink, NULL) /* new function */ PHP_FE(pdf_add_launchlink, NULL)/* new function */ PHP_FE(pdf_add_weblink, NULL) PHP_FE(pdf_set_border_style, NULL) PHP_FE(pdf_set_border_color, NULL) PHP_FE(pdf_set_border_dash, NULL) /* End of the official PDFLIB V3.x API */ /* aliases for compatibility reasons */ PHP_FALIAS(pdf_add_outline, pdf_add_bookmark, NULL) /* old font handling */ PHP_FE(pdf_set_font, NULL) /* deprecated */ PHP_FE(pdf_get_font, NULL) /* deprecated */ PHP_FE(pdf_get_fontname, NULL) /* deprecated */ PHP_FE(pdf_get_fontsize, NULL) /* deprecated */ /* old way of starting a PDF document */ PHP_FE(pdf_open, NULL) /* deprecated */ /* old stuff for setting infos */ PHP_FE(pdf_set_info_creator, NULL) /* deprecated */ PHP_FE(pdf_set_info_title, NULL) /* deprecated */ PHP_FE(pdf_set_info_subject, NULL) /* deprecated */ PHP_FE(pdf_set_info_author, NULL) /* deprecated */ PHP_FE(pdf_set_info_keywords, NULL) /* deprecated */ PHP_FE(pdf_set_leading, NULL) /* deprecated */ PHP_FE(pdf_set_text_rendering, NULL) /* deprecated */ PHP_FE(pdf_set_horiz_scaling, NULL) /* deprecated */ PHP_FE(pdf_set_text_rise, NULL) /* deprecated */ PHP_FE(pdf_set_char_spacing, NULL) /* deprecated */ PHP_FE(pdf_set_word_spacing, NULL) /* deprecated */ PHP_FE(pdf_set_transition, NULL) /* deprecated */ PHP_FE(pdf_set_duration, NULL) /* deprecated */ PHP_FE(pdf_get_image_height, NULL) /* deprecated */ PHP_FE(pdf_get_image_width, NULL) /* deprecated */ /* old stuff for opening images */ PHP_FE(pdf_open_jpeg, NULL) /* deprecated */ PHP_FE(pdf_open_tiff, NULL) /* deprecated */ PHP_FE(pdf_open_png, NULL) /* deprecated */ PHP_FE(pdf_open_gif, NULL) /* deprecated */ /* some more stuff for compatibility */ PHP_FE(pdf_add_annotation, NULL)#if HAVE_LIBGD13 PHP_FE(pdf_open_memory_image, NULL)#endif /* depreciatet after V4.0 of PDFlib */ PHP_FE(pdf_setgray_fill, NULL) PHP_FE(pdf_setgray_stroke, NULL) PHP_FE(pdf_setgray, NULL) PHP_FE(pdf_setrgbcolor_fill, NULL) PHP_FE(pdf_setrgbcolor_stroke, NULL) PHP_FE(pdf_setrgbcolor, NULL)#if (PDFLIB_MAJORVERSION >= 4)/* support for new functions in PDFlib V4.0 */ PHP_FE(pdf_open_pdi, NULL) PHP_FE(pdf_close_pdi, NULL) PHP_FE(pdf_open_pdi_page, NULL) PHP_FE(pdf_place_pdi_page, NULL) PHP_FE(pdf_close_pdi_page, NULL) PHP_FE(pdf_get_pdi_parameter, NULL) PHP_FE(pdf_get_pdi_value, NULL) PHP_FE(pdf_begin_pattern, NULL) PHP_FE(pdf_end_pattern, NULL) PHP_FE(pdf_begin_template, NULL) PHP_FE(pdf_end_template, NULL) PHP_FE(pdf_setcolor, NULL) PHP_FE(pdf_makespotcolor, NULL) PHP_FE(pdf_arcn, NULL) PHP_FE(pdf_add_thumbnail, NULL) PHP_FE(pdf_initgraphics, NULL) PHP_FE(pdf_setmatrix, NULL)#endif /* PDFlib >= V4 */ {NULL, NULL, NULL}};/* }}} *//* {{{ pdf_module_entry */zend_module_entry pdf_module_entry = { STANDARD_MODULE_HEADER, "pdf", pdf_functions, PHP_MINIT(pdf), PHP_MSHUTDOWN(pdf), NULL, NULL, PHP_MINFO(pdf), NO_VERSION_YET, STANDARD_MODULE_PROPERTIES };/* }}} */#ifdef COMPILE_DL_PDFZEND_GET_MODULE(pdf)#endif/* {{{ _free_pdf_doc */static void _free_pdf_doc(zend_rsrc_list_entry *rsrc TSRMLS_DC){ PDF *pdf = (PDF *)rsrc->ptr; PDF_delete(pdf);}/* }}} *//* {{{ custom_errorhandler */static void custom_errorhandler(PDF *p, int type, const char *shortmsg){ switch (type){ case PDF_NonfatalError: /* * PDFlib warnings should be visible to the user. * If he decides to live with PDFlib warnings * he may use the PDFlib function * pdf_set_parameter($p, "warning" 0) to switch off * the warnings inside PDFlib. */ php_error(E_WARNING,"Internal PDFlib warning: %s", shortmsg); return; case PDF_MemoryError: /* give up in all other cases */ case PDF_IOError: case PDF_RuntimeError: case PDF_IndexError: case PDF_TypeError: case PDF_DivisionByZero: case PDF_OverflowError: case PDF_SyntaxError: case PDF_ValueError: case PDF_SystemError: case PDF_UnknownError: default: php_error(E_ERROR,"PDFlib error: %s", shortmsg); }}/* }}} *//* {{{ pdf_emalloc */static void *pdf_emalloc(PDF *p, size_t size, const char *caller){ return(emalloc(size));}/* }}} *//* {{{ pdf_realloc */static void *pdf_realloc(PDF *p, void *mem, size_t size, const char *caller){ return(erealloc(mem, size));}/* }}} *//* {{{ pdf_efree */static void pdf_efree(PDF *p, void *mem){ efree(mem);}/* }}} *//* {{{ pdf_flushwrite */static size_t pdf_flushwrite(PDF *p, void *data, size_t size){ TSRMLS_FETCH(); return(php_write(data, size TSRMLS_CC));}/* }}} *//* {{{ PHP_MINFO_FUNCTION */PHP_MINFO_FUNCTION(pdf){ char tmp[32]; snprintf(tmp, 31, "%d.%02d", PDF_get_majorversion(), PDF_get_minorversion() ); tmp[31]=0; php_info_print_table_start(); php_info_print_table_row(2, "PDF Support", "enabled" );#if (PDFLIB_MAJORVERSION >= 4) php_info_print_table_row(2, "PDFlib GmbH Version", PDFLIB_VERSIONSTRING );#else php_info_print_table_row(2, "PDFlib GmbH Version", tmp );#endif php_info_print_table_row(2, "Revision", "$Revision: 1.112.2.11.2.2 $" ); php_info_print_table_end();}/* }}} *//* {{{ PHP_MINIT_FUNCTION */PHP_MINIT_FUNCTION(pdf){ if ((PDF_get_majorversion() != PDFLIB_MAJORVERSION) || (PDF_get_minorversion() != PDFLIB_MINORVERSION)) { php_error(E_ERROR,"PDFlib error: Version mismatch in wrapper code"); } le_pdf = zend_register_list_destructors_ex(_free_pdf_doc, NULL, "pdf object", module_number); /* this does something like setlocale("C", ...) in PDFlib 3.x */ PDF_boot(); return SUCCESS;}/* }}} *//* {{{ PHP_MSHUTDOWN_FUNCTION */PHP_MSHUTDOWN_FUNCTION(pdf){ PDF_shutdown(); return SUCCESS;}/* }}} *//* {{{ _php_pdf_set_info */static void _php_pdf_set_info(INTERNAL_FUNCTION_PARAMETERS, char *field) { zval **arg1, **arg2; PDF *pdf; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(pdf, PDF *, arg1, -1, "pdf object", le_pdf); convert_to_string_ex(arg2); PDF_set_info(pdf, field, Z_STRVAL_PP(arg2)); RETURN_TRUE;}/* }}} *//* {{{ proto bool pdf_set_info(int pdfdoc, string fieldname, string value) Fills an info field of the document */PHP_FUNCTION(pdf_set_info){ zval **arg1, **arg2, **arg3; PDF *pdf; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(pdf, PDF *, arg1, -1, "pdf object", le_pdf); convert_to_string_ex(arg2); convert_to_string_ex(arg3); PDF_set_info(pdf, Z_STRVAL_PP(arg2), Z_STRVAL_PP(arg3)); RETURN_TRUE;}/* }}} *//* {{{ proto bool pdf_set_info_creator(int pdfdoc, string creator) Fills the creator field of the document */PHP_FUNCTION(pdf_set_info_creator){ _php_pdf_set_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Creator");}/* }}} *//* {{{ proto bool pdf_set_info_title(int pdfdoc, string title) Fills the title field of the document */PHP_FUNCTION(pdf_set_info_title) { _php_pdf_set_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Title");}/* }}} *//* {{{ proto bool pdf_set_info_subject(int pdfdoc, string subject) Fills the subject field of the document */PHP_FUNCTION(pdf_set_info_subject) { _php_pdf_set_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Subject");}/* }}} *//* {{{ proto bool pdf_set_info_author(int pdfdoc, string author) Fills the author field of the document */PHP_FUNCTION(pdf_set_info_author) { _php_pdf_set_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Author");}/* }}} *//* {{{ proto bool pdf_set_info_keywords(int pdfdoc, string keywords) Fills the keywords field of the document */PHP_FUNCTION(pdf_set_info_keywords){ _php_pdf_set_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Keywords");}/* }}} *//* {{{ proto int pdf_open([int filedesc]) Opens a new pdf document. If filedesc is NULL, document is created in memory. This is the old interface, only for compatibility use pdf_new + pdf_open_file instead */PHP_FUNCTION(pdf_open){ zval **file; FILE *fp = NULL; PDF *pdf; int argc = ZEND_NUM_ARGS(); if(argc > 1) { WRONG_PARAM_COUNT; } else if (argc != 1 || zend_get_parameters_ex(1, &file) == FAILURE) { fp = NULL; } else { php_stream *stream; php_stream_from_zval(stream, file); if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void*)&fp, 1) == FAILURE) { RETURN_FALSE; } } pdf = PDF_new2(custom_errorhandler, pdf_emalloc, pdf_realloc, pdf_efree, NULL); if(fp) { if (PDF_open_fp(pdf, fp) < 0) { RETURN_FALSE; } } else { PDF_open_mem(pdf, pdf_flushwrite); }#if (PDFLIB_MAJORVERSION >= 4) PDF_set_parameter(pdf, "imagewarning", "true");#endif PDF_set_parameter(pdf, "binding", "PHP"); ZEND_REGISTER_RESOURCE(return_value, pdf, le_pdf);}/* }}} *//* {{{ proto void pdf_close(int pdfdoc) Closes the pdf document */PHP_FUNCTION(pdf_close){ zval **arg1; PDF *pdf; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(pdf, PDF *, arg1, -1, "pdf object", le_pdf); PDF_close(pdf); RETURN_TRUE;}/* }}} *//* {{{ proto void pdf_begin_page(int pdfdoc, float width, float height) Starts page */PHP_FUNCTION(pdf_begin_page){ zval **arg1, **arg2, **arg3; PDF *pdf; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) { WRONG_PARAM_COUNT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -