@@ -4781,6 +4781,83 @@ void JM_ScanResources(fz_context *ctx, pdf_document *pdf, pdf_obj *rsrc,
4781
4781
}
4782
4782
4783
4783
4784
+
4785
+ //-----------------------------------------------------------------------------
4786
+ // convert a document to a PDF
4787
+ //-----------------------------------------------------------------------------
4788
+ PyObject *JM_convert_to_pdf(fz_context *ctx, fz_document *doc)
4789
+ {
4790
+ pdf_document *pdfout = pdf_create_document(ctx);
4791
+ int pageCount = fz_count_pages(ctx, doc);
4792
+ fz_rect mediabox;
4793
+ fz_device *dev = NULL;
4794
+ fz_buffer *contents = NULL;
4795
+ pdf_obj *resources = NULL;
4796
+ fz_page *page;
4797
+ int i;
4798
+ for (i = 0; i < pageCount; i++)
4799
+ {
4800
+ fz_try(ctx)
4801
+ {
4802
+ page = fz_load_page(ctx, doc, i);
4803
+ fz_bound_page(ctx, page, &mediabox);
4804
+ dev = pdf_page_write(ctx, pdfout, &mediabox, &resources, &contents);
4805
+ fz_run_page(ctx, page, dev, &fz_identity, NULL);
4806
+ fz_close_device(ctx, dev);
4807
+ fz_drop_device(ctx, dev);
4808
+ dev = NULL;
4809
+ pdf_obj *page_obj = pdf_add_page(ctx, pdfout, &mediabox, 0, resources, contents);
4810
+ pdf_insert_page(ctx, pdfout, -1, page_obj);
4811
+ pdf_drop_obj(ctx, page_obj);
4812
+ }
4813
+ fz_always(ctx)
4814
+ {
4815
+ pdf_drop_obj(ctx, resources);
4816
+ fz_drop_buffer(ctx, contents);
4817
+ fz_drop_device(ctx, dev);
4818
+ }
4819
+ fz_catch(ctx)
4820
+ {
4821
+ fz_drop_page(ctx, page);
4822
+ fz_rethrow(ctx);
4823
+ }
4824
+ }
4825
+ unsigned char *c;
4826
+ PyObject *r;
4827
+ size_t len;
4828
+ fz_buffer *res = NULL;
4829
+ fz_output *out = NULL;
4830
+ int errors = 0;
4831
+ pdf_write_options opts;
4832
+ opts.do_incremental = 0;
4833
+ opts.do_ascii = 0;
4834
+ opts.do_compress = 1;
4835
+ opts.do_compress_images = 1;
4836
+ opts.do_compress_fonts = 1;
4837
+ opts.do_decompress = 0;
4838
+ opts.do_garbage = 4;
4839
+ opts.do_linear = 0;
4840
+ opts.do_clean = 0;
4841
+ opts.do_pretty = 0;
4842
+ opts.continue_on_error = 1;
4843
+ opts.errors = &errors;
4844
+ fz_try(ctx)
4845
+ {
4846
+ res = fz_new_buffer(ctx, 1024);
4847
+ out = fz_new_output_with_buffer(ctx, res);
4848
+ pdf_write_document(ctx, pdfout, out, &opts);
4849
+ len = fz_buffer_storage(ctx, res, &c);
4850
+ r = PyBytes_FromStringAndSize(c, len);
4851
+ }
4852
+ fz_always(ctx)
4853
+ {
4854
+ fz_drop_output(ctx, out);
4855
+ fz_drop_buffer(ctx, res);
4856
+ }
4857
+ fz_catch(ctx) fz_rethrow(ctx);
4858
+ return r;
4859
+ }
4860
+
4784
4861
SWIGINTERN void delete_fz_document_s(struct fz_document_s *self){
4785
4862
DEBUGMSG1("document w/o close");
4786
4863
fz_drop_document(gctx, self);
@@ -5366,6 +5443,15 @@ SWIGINTERN int fz_document_s_embeddedFileAdd(struct fz_document_s *self,PyObject
5366
5443
fz_catch(gctx) return -1;
5367
5444
return entry;
5368
5445
}
5446
+ SWIGINTERN PyObject *fz_document_s_convertToPDF(struct fz_document_s *self){
5447
+ PyObject *doc;
5448
+ fz_try(gctx)
5449
+ {
5450
+ doc = JM_convert_to_pdf(gctx, self);
5451
+ }
5452
+ fz_catch(gctx) return NULL;
5453
+ return doc;
5454
+ }
5369
5455
SWIGINTERN int fz_document_s_pageCount(struct fz_document_s *self){
5370
5456
return fz_count_pages(gctx, self);
5371
5457
}
@@ -5992,12 +6078,19 @@ SWIGINTERN PyObject *fz_document_s_isFormPDF(struct fz_document_s *self){
5992
6078
pdf_document *pdf = pdf_specifics(gctx, self);
5993
6079
if (!pdf) Py_RETURN_FALSE;
5994
6080
pdf_obj *form = NULL;
6081
+ pdf_obj *fields = NULL;
6082
+ int have_form = 0;
5995
6083
fz_try(gctx)
5996
6084
{
5997
6085
form = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, PDF_NAME_AcroForm, NULL);
6086
+ if (form)
6087
+ {
6088
+ fields = pdf_dict_get(gctx, form, PDF_NAME_Fields);
6089
+ if (fields && pdf_array_len(gctx, fields) > 0) have_form = 1;
6090
+ }
5998
6091
}
5999
6092
fz_catch(gctx) Py_RETURN_FALSE;
6000
- if (!form ) Py_RETURN_FALSE;
6093
+ if (!have_form ) Py_RETURN_FALSE;
6001
6094
Py_RETURN_TRUE;
6002
6095
}
6003
6096
SWIGINTERN int fz_document_s__getOLRootNumber(struct fz_document_s *self){
@@ -9131,6 +9224,35 @@ SWIGINTERN PyObject *_wrap_Document_embeddedFileAdd(PyObject *SWIGUNUSEDPARM(sel
9131
9224
}
9132
9225
9133
9226
9227
+ SWIGINTERN PyObject *_wrap_Document_convertToPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
9228
+ PyObject *resultobj = 0;
9229
+ struct fz_document_s *arg1 = (struct fz_document_s *) 0 ;
9230
+ void *argp1 = 0 ;
9231
+ int res1 = 0 ;
9232
+ PyObject * obj0 = 0 ;
9233
+ PyObject *result = 0 ;
9234
+
9235
+ if (!PyArg_ParseTuple(args,(char *)"O:Document_convertToPDF",&obj0)) SWIG_fail;
9236
+ res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 );
9237
+ if (!SWIG_IsOK(res1)) {
9238
+ SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_convertToPDF" "', argument " "1"" of type '" "struct fz_document_s *""'");
9239
+ }
9240
+ arg1 = (struct fz_document_s *)(argp1);
9241
+ {
9242
+ result = (PyObject *)fz_document_s_convertToPDF(arg1);
9243
+ if(!result)
9244
+ {
9245
+ PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx));
9246
+ return NULL;
9247
+ }
9248
+ }
9249
+ resultobj = result;
9250
+ return resultobj;
9251
+ fail:
9252
+ return NULL;
9253
+ }
9254
+
9255
+
9134
9256
SWIGINTERN PyObject *_wrap_Document_pageCount(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
9135
9257
PyObject *resultobj = 0;
9136
9258
struct fz_document_s *arg1 = (struct fz_document_s *) 0 ;
@@ -18018,6 +18140,7 @@ static PyMethodDef SwigMethods[] = {
18018
18140
{ (char *)"Document_embeddedFileSetInfo", _wrap_Document_embeddedFileSetInfo, METH_VARARGS, (char *)"Change filename or description of embedded file given its entry number or name."},
18019
18141
{ (char *)"Document_embeddedFileGet", _wrap_Document_embeddedFileGet, METH_VARARGS, (char *)"Retrieve embedded file content given its entry number or name."},
18020
18142
{ (char *)"Document_embeddedFileAdd", _wrap_Document_embeddedFileAdd, METH_VARARGS, (char *)"Add new file from buffer."},
18143
+ { (char *)"Document_convertToPDF", _wrap_Document_convertToPDF, METH_VARARGS, (char *)"Document_convertToPDF(self) -> PyObject *"},
18021
18144
{ (char *)"Document_pageCount", _wrap_Document_pageCount, METH_VARARGS, (char *)"Document_pageCount(self) -> int"},
18022
18145
{ (char *)"Document__getMetadata", _wrap_Document__getMetadata, METH_VARARGS, (char *)"Document__getMetadata(self, key) -> char *"},
18023
18146
{ (char *)"Document_needsPass", _wrap_Document_needsPass, METH_VARARGS, (char *)"Document_needsPass(self) -> int"},
0 commit comments