pdf.c

来自「windows下PDF文档的开发包」· C语言 代码 · 共 2,468 行 · 第 1/5 页

C
2,468
字号
/*
   +----------------------------------------------------------------------+
   | PHP version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2003 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.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.30.2.4 2003/09/10 17:43:27 rjs Exp $ */

#ifdef PDF_FEATURE_PUBLIC
#else   /* !PDF_FEATURE_PUBLIC */
/* Bootstrap of PDFlib Feature setup */
#define PDF_FEATURE_SERIAL
#endif  /* PDF_FEATURE_PUBLIC */

#ifdef PDF_FEATURE_SERIAL
/*
	* synced with pdflib.h 1.135

	This file contains the PDFlib GmbH supported code for the PDFlib
	PHP-wrapper.
	We suply this code, because the version of ext/pdf/pdf.c that comes with
	PHP does only work with the latest version of PHP, so if you are stuck
	to some older version of PHP you might not be able to use PDFlib V5.

	If you are in doubt if this version is fine for you, you may check the
	CVS-Version this file is derived from (see below) and check the PHP-CVS
	for more recent changes to this file.


	NOTE:
		We dropped all (very) old API's from wrappers created before PDFlib
		GmbH started to support this wrapper officially with this version.

		Till now we included the (very) old API's for compatibility reasons,
		but to simplyfy the maintainace of this wrapper, we decided to drop
		this with the PDFlib V5 wrapper now.

		This does not affect the pdf_open_memory_image() API, which really
		added new functionality.  PDFlib GmbH does not support this,
		but includes this valuable work. 

		And we dropped the virtual_dir support. This feature is quite
		confusing as it is not working on all platforms and configurations.
		And it conflicts with the new "SearchPath" feature of PDFlib. It
		also did not work when creating PDF-Files with PDF_open_file().
*/
#endif /* PDF_FEATURE_SERIAL */

/* derived from:
    Id: pdf.c,v 1.112.2.5 2003/04/30 21:54:02 iliaa Exp

    with some exeptions:
    - pdf_get_major/minorversion not included, as pdf_get_value supports this
      without a PDF-object
    - #if ZEND_MODULE_API_NO >= 20010901 for new ZEND_MODULE support,
      so that it compiles with older PHP Versions too
    - TSRMLS fixes included only with ZEND_MODULE_API_NO >= 20010901
      would break older builds otherwise
	- new PHP streams #if ZEND_MODULE_API_NO >= 20020429 && HAVE_PHP_STREAM
	  TODO: CVS (1.107/108/109/112)
	- added better support to see if it comes as binary from PDFlib GmbH.
	- change from emalloc to safe_emalloc (112.2.4 -> 112.2.5) not included
	  (would not be backwardcompatible with older php builds)
	- TODO: merge back to PHP-CVS
   */

/* pdflib 2.02 ... 4.0x is subject to the ALADDIN FREE PUBLIC LICENSE.
   Copyright (C) 1997-1999 Thomas Merz. 2000-2002 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"

#if HAVE_LIBGD13
#include "ext/gd/php_gd.h"
#if HAVE_GD_BUNDLED 
#include "ext/gd/libgd/gd.h" 
#else 
#include "gd.h"
#endif
static 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"

#if ((100*PDFLIB_MAJORVERSION+10*PDFLIB_MINORVERSION+PDFLIB_REVISION) >= 500)
	/* This wrapper code will work only with PDFlib V5 or greater,
	 * because the special handling for returning 0 instead of -1
	 * for PHP is now done in the PDFlib kernel
	 */
#else

#error "PDFlib version does not match PHP-wrapper code"

#endif /* PDFlib >= V5 */

#undef VIRTUAL_DIR

static int le_pdf;

/* {{{ pdf_functions[]
 */
function_entry pdf_functions[] = {
	/* p_font.c */
	PHP_FE(pdf_add_launchlink, NULL)
	PHP_FE(pdf_add_locallink, NULL)
	PHP_FE(pdf_add_note, NULL)
	PHP_FE(pdf_add_pdflink, NULL)
	PHP_FE(pdf_add_weblink, NULL)
	PHP_FE(pdf_attach_file, NULL)
	PHP_FE(pdf_set_border_color, NULL)
	PHP_FE(pdf_set_border_dash, NULL)
	PHP_FE(pdf_set_border_style, NULL)

	/* p_basic.c */
	PHP_FE(pdf_begin_page, NULL)
	PHP_FE(pdf_close, NULL)
	PHP_FE(pdf_delete, NULL)
	PHP_FE(pdf_end_page, NULL)
	PHP_FE(pdf_get_buffer, NULL)
	PHP_FE(pdf_new, NULL)
	PHP_FE(pdf_open_file, NULL)

	/* p_block.c */
	PHP_FE(pdf_fill_imageblock, NULL)
	PHP_FE(pdf_fill_pdfblock, NULL)
	PHP_FE(pdf_fill_textblock, NULL)

	/* p_color.c */
	PHP_FE(pdf_makespotcolor, NULL)
	PHP_FE(pdf_setcolor, NULL)
	PHP_FE(pdf_setgray_fill, NULL)		/* deprecated (since 4.0) */
	PHP_FE(pdf_setgray_stroke, NULL)	/* deprecated (since 4.0) */
	PHP_FE(pdf_setgray, NULL)			/* deprecated (since 4.0) */
	PHP_FE(pdf_setrgbcolor_fill, NULL)	/* deprecated (since 4.0) */
	PHP_FE(pdf_setrgbcolor_stroke, NULL)/* deprecated (since 4.0) */
	PHP_FE(pdf_setrgbcolor, NULL)		/* deprecated (since 4.0) */

	/* p_draw.c */
	PHP_FE(pdf_arc, NULL)
	PHP_FE(pdf_arcn, NULL)
	PHP_FE(pdf_circle, NULL)
	PHP_FE(pdf_clip, NULL)
	PHP_FE(pdf_closepath, NULL)
	PHP_FE(pdf_closepath_fill_stroke, NULL)
	PHP_FE(pdf_closepath_stroke, NULL)
	PHP_FE(pdf_curveto, NULL)
	PHP_FE(pdf_endpath, NULL)
	PHP_FE(pdf_fill, NULL)
	PHP_FE(pdf_fill_stroke, NULL)
	PHP_FE(pdf_lineto, NULL)
	PHP_FE(pdf_moveto, NULL)
	PHP_FE(pdf_rect, NULL)
	PHP_FE(pdf_stroke, NULL)

	/* p_encoding.c */
	PHP_FE(pdf_encoding_set_char, NULL)

	/* p_font.c */
	PHP_FE(pdf_findfont, NULL)
	PHP_FE(pdf_load_font, NULL)
	PHP_FE(pdf_setfont, NULL)

	/* p_gstate.c */
	PHP_FE(pdf_concat, NULL)
	PHP_FE(pdf_initgraphics, NULL)
	PHP_FE(pdf_restore, NULL)
	PHP_FE(pdf_rotate, NULL)
	PHP_FE(pdf_save, NULL)
	PHP_FE(pdf_scale, NULL)
	PHP_FE(pdf_setdash, NULL)
	PHP_FE(pdf_setdashpattern, NULL)
	PHP_FE(pdf_setflat, NULL)
	PHP_FE(pdf_setlinecap, NULL)
	PHP_FE(pdf_setlinejoin, NULL)
	PHP_FE(pdf_setlinewidth, NULL)
	PHP_FE(pdf_setmatrix, NULL)
	PHP_FE(pdf_setmiterlimit, NULL)
	PHP_FE(pdf_setpolydash, NULL)		/* deprecated since V5.0 */
	PHP_FE(pdf_skew, NULL)
	PHP_FE(pdf_translate, NULL)

	/* p_hyper.c */
	PHP_FE(pdf_add_bookmark, NULL)
	PHP_FE(pdf_add_nameddest, NULL)
	PHP_FE(pdf_set_info, NULL)

	/* p_icc.c */
	PHP_FE(pdf_load_iccprofile, NULL)

	/* p_image.c */
	PHP_FE(pdf_add_thumbnail, NULL)
	PHP_FE(pdf_close_image, NULL)
	PHP_FE(pdf_fit_image, NULL)
	PHP_FE(pdf_load_image, NULL)
	PHP_FE(pdf_open_ccitt, NULL)		/* deprecated since V5.0 */
	PHP_FE(pdf_open_image, NULL)		/* deprecated since V5.0 */
	PHP_FE(pdf_open_image_file, NULL)	/* deprecated since V5.0 */
	PHP_FE(pdf_place_image, NULL)		/* deprecated since V5.0 */

	/* p_params.c */
	PHP_FE(pdf_get_parameter, NULL)
	PHP_FE(pdf_get_value, NULL)
	PHP_FE(pdf_set_parameter, NULL)
	PHP_FE(pdf_set_value, NULL)

	/* p_pattern.c */
	PHP_FE(pdf_begin_pattern, NULL)
	PHP_FE(pdf_end_pattern, NULL)

	/* p_pdi.c */
	PHP_FE(pdf_close_pdi, NULL)
	PHP_FE(pdf_close_pdi_page, NULL)
	PHP_FE(pdf_fit_pdi_page, NULL)
	PHP_FE(pdf_get_pdi_parameter, NULL)
	PHP_FE(pdf_get_pdi_value, NULL)
	PHP_FE(pdf_open_pdi, NULL)
	PHP_FE(pdf_open_pdi_page, NULL)
	PHP_FE(pdf_place_pdi_page, NULL)	/* deprecated since V5.0 */
	PHP_FE(pdf_process_pdi, NULL)

	/* p_resource.c */
	PHP_FE(pdf_create_pvf, NULL)
	PHP_FE(pdf_delete_pvf, NULL)

	/* p_shading.c */
	PHP_FE(pdf_shading, NULL)
	PHP_FE(pdf_shading_pattern, NULL)
	PHP_FE(pdf_shfill, NULL)

	/* p_template.c */
	PHP_FE(pdf_begin_template, NULL)
	PHP_FE(pdf_end_template, NULL)

	/* p_text.c */
	PHP_FE(pdf_continue_text, NULL)
	PHP_FE(pdf_fit_textline, NULL)
	PHP_FE(pdf_set_text_pos, NULL)
	PHP_FE(pdf_show, NULL)
	PHP_FE(pdf_show_boxed, NULL)
	PHP_FE(pdf_show_xy, NULL)
	PHP_FE(pdf_stringwidth, NULL)

	/* p_type3.c */
	PHP_FE(pdf_begin_font, NULL)
	PHP_FE(pdf_begin_glyph, NULL)
	PHP_FE(pdf_end_font, NULL)
	PHP_FE(pdf_end_glyph, NULL)

	/* p_xgstate.c */
	PHP_FE(pdf_create_gstate, NULL)
	PHP_FE(pdf_set_gstate, NULL)

	/* exception handling */
	PHP_FE(pdf_get_errnum, NULL)
	PHP_FE(pdf_get_errmsg, NULL)
	PHP_FE(pdf_get_apiname, NULL)

	/* End of the official PDFLIB V3.x/V4.x/V5.x API */

#if HAVE_LIBGD13
	/* not supported by PDFlib GmbH */
	PHP_FE(pdf_open_memory_image, NULL)
#endif

	{NULL, NULL, NULL}
};
/* }}} */

/* {{{ pdf_module_entry
 */
zend_module_entry pdf_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
	"pdf", 
	pdf_functions, 
	PHP_MINIT(pdf), 
	PHP_MSHUTDOWN(pdf), 
	NULL, 
	NULL, 
	PHP_MINFO(pdf), 
#if ZEND_MODULE_API_NO >= 20010901
    NO_VERSION_YET,
#endif
	STANDARD_MODULE_PROPERTIES 
};
/* }}} */

#ifdef COMPILE_DL_PDF
ZEND_GET_MODULE(pdf)
#endif

/* PHP/PDFlib internal functions */
/* {{{ _free_pdf_doc
 */
static void _free_pdf_doc(zend_rsrc_list_entry *rsrc)
{
	PDF *pdf = (PDF *)rsrc->ptr;
	PDF_delete(pdf);
}
/* }}} */

/* {{{ custom_errorhandler
 */
static void custom_errorhandler(PDF *p, int errnum, const char *shortmsg)
{
    if (errnum == 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, "PDFlib warning %s", shortmsg);
    }
    else
    {
        /* give up in all other cases */
		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);
}
/* }}} */

/* {{{ 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" );
#ifdef PDF_FEATURE_SERIAL
	php_info_print_table_row(2, "PDFlib GmbH Binary-Version", PDFLIB_VERSIONSTRING );
#else /* PDF_FEATURE_SERIAL */
	php_info_print_table_row(2, "PDFlib GmbH Version", PDFLIB_VERSIONSTRING );
#endif /* PDF_FEATURE_SERIAL */
	php_info_print_table_row(2, "Revision", "$Revision: 1.30.2.4 $" );
	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;
}
/* }}} */


/* p_annots.c */

/* {{{ proto void pdf_add_launchlink(int pdfdoc, float llx, float lly, float urx, float ury, string filename)
 * Add a launch annotation (to a target of arbitrary file type). */
PHP_FUNCTION(pdf_add_launchlink)
{
	zval **p, **llx, **lly, **urx, **ury, **filename;
	PDF *pdf;
	const char * vfilename;

	if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &p, &llx, &lly, &urx, &ury, &filename) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	ZEND_FETCH_RESOURCE(pdf, PDF *, p, -1, "pdf object", le_pdf);

	convert_to_double_ex(llx);
	convert_to_double_ex(lly);
	convert_to_double_ex(urx);
	convert_to_double_ex(ury);
	convert_to_string_ex(filename);

#ifdef VIRTUAL_DIR
#    if ZEND_MODULE_API_NO >= 20010901
	virtual_filepath(Z_STRVAL_PP(filename), &vfilename TSRMLS_CC);
#    else
	virtual_filepath(Z_STRVAL_PP(filename), &vfilename);
#    endif
#else
	vfilename = Z_STRVAL_PP(filename);
#endif  

	PDF_add_launchlink(pdf,
		(float) Z_DVAL_PP(llx),
		(float) Z_DVAL_PP(lly),
		(float) Z_DVAL_PP(urx),
		(float) Z_DVAL_PP(ury),
		vfilename);

	RETURN_TRUE;
}
/* }}} */

/* TODO [optlist] */
/* {{{ proto void pdf_add_locallink(int pdfdoc, float llx, float lly, float urx, float ury, int page, string optlist)
 * Add a link annotation to a target within the current PDF file. */
PHP_FUNCTION(pdf_add_locallink)
{
	zval **p, **llx, **lly, **urx, **ury, **page, **optlist;
	PDF *pdf;

	if (ZEND_NUM_ARGS() != 7 || zend_get_parameters_ex(7, &p, &llx, &lly, &urx, &ury, &page, &optlist) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	ZEND_FETCH_RESOURCE(pdf, PDF *, p, -1, "pdf object", le_pdf);

	convert_to_double_ex(llx);
	convert_to_double_ex(lly);
	convert_to_double_ex(urx);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?