Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use it for split and merge PDF's pages? #5

Open
Aminadav opened this issue Aug 16, 2023 · 1 comment
Open

How can I use it for split and merge PDF's pages? #5

Aminadav opened this issue Aug 16, 2023 · 1 comment

Comments

@Aminadav
Copy link

No description provided.

@Aminadav Aminadav changed the title How can I use it for split and merge PDF's? How can I use it for split and merge PDF's pages? Aug 16, 2023
@insinfo
Copy link
Owner

insinfo commented Aug 17, 2023

here is a possible way
you can create extensions for missing methods in PDFiumBindings class
if you want you can also contribute with a PR

https://github.com/klippa-app/pdfium-cli/blob/main/cmd/merge.go
https://github.com/chromium/pdfium/blob/4ae353f1e22efea86262f9cdd4f0e8478f142182/public/fpdf_ppo.h#L49
https://pdfium.patagames.com/

import 'package:pdfium_bindings/pdfium_bindings.dart';
import 'package:ffi/ffi.dart';
import 'dart:ffi';



typedef _c_FPDF_LoadDocument = Pointer<fpdf_document_t__> Function(
  Pointer<Int8> file_path,
  Pointer<Int8> password,
);

typedef _dart_FPDF_LoadDocument = Pointer<fpdf_document_t__> Function(
  Pointer<Int8> file_path,
  Pointer<Int8> password,
);

// Basic data types
typedef FPDF_BYTESTRING = Pointer<Int8>;
typedef FPDF_BOOL = Int32;

/// function signature in the C API of the PDFium FPDF_ImportPages function
typedef _c_FPDF_ImportPages = FPDF_BOOL Function(
  Pointer<fpdf_document_t__> dest_doc,
  Pointer<fpdf_document_t__> src_doc,
  FPDF_BYTESTRING pagerange,
  Int32 index,
);

/// equivalent signature in dart of PDFium FPDF_ImportPages function
typedef _dart_FPDF_ImportPages = int Function(
  Pointer<fpdf_document_t__> dest_doc,
  Pointer<fpdf_document_t__> src_doc,
  FPDF_BYTESTRING pagerange,
  int index,
);

/// example extension that adds the FPDF_ImportPages function
extension PDFiumBindingsExtensions on PDFiumBindings {
  Pointer<fpdf_document_t__> FPDF_LoadDocument2(Pointer<Int8> file_path,
      Pointer<Int8> password, DynamicLibrary dynamicLibrary) {
    final _lookup = dynamicLibrary.lookup;

    final _FPDF_LoadDocument_ptr =
        _lookup<NativeFunction<_c_FPDF_LoadDocument>>('FPDF_LoadDocument');

    final _dart_FPDF_LoadDocument _FPDF_LoadDocument =
        _FPDF_LoadDocument_ptr.asFunction<_dart_FPDF_LoadDocument>();

    return _FPDF_LoadDocument(
      file_path,
      password,
    );
  }

  /// Import pages to a FPDF_DOCUMENT.
  ///
  ///   dest_doc  - The destination document for the pages.
  ///   src_doc   - The document to be imported.
  ///   pagerange - A page range string, Such as "1,3,5-7". The first page is one.
  ///               If |pagerange| is NULL, all pages from |src_doc| are imported.
  ///   index     - The page index at which to insert the first imported page into
  ///               |dest_doc|. The first page is zero.
  ///
  /// Returns TRUE on success. Returns FALSE if any pages in |pagerange| is
  /// invalid or if |pagerange| cannot be read.
  /// https://github.com/chromium/pdfium/blob/4ae353f1e22efea86262f9cdd4f0e8478f142182/public/fpdf_ppo.h#L49
  int FPDF_ImportPages(Pointer<fpdf_document_t__> dest_doc, Pointer<fpdf_document_t__> src_doc,
      FPDF_BYTESTRING pagerange, int index, DynamicLibrary dynamicLibrary) {
    final _lookup = dynamicLibrary.lookup;

    final _FPDF_ImportPages_ptr =
        _lookup<NativeFunction<_c_FPDF_ImportPages>>('FPDF_ImportPages');

    final _dart_FPDF_ImportPages _FPDF_ImportPages =
        _FPDF_ImportPages_ptr.asFunction<_dart_FPDF_ImportPages>();

    return _FPDF_ImportPages(
      dest_doc,
      src_doc,
      pagerange,
      index,
    );
  }



}


import 'dart:ffi';
import 'dart:io';

import 'package:certificadox509/teste.dart';
import 'package:ffi/ffi.dart';
import 'package:image/image.dart';
import 'package:path/path.dart' as path;
import 'package:pdfium_bindings/pdfium_bindings.dart';

void main() {
  final libraryPath = path.join(Directory.current.path, 'pdfium.dll');
  final dll = DynamicLibrary.open(libraryPath);
  final pdfium = PDFiumBindings(dll);

  const allocate = calloc;

  final config = allocate<FPDF_LIBRARY_CONFIG>();
  config.ref.version = 2;
  config.ref.m_pUserFontPaths = nullptr;
  config.ref.m_pIsolate = nullptr;
  config.ref.m_v8EmbedderSlot = 0;
  pdfium.FPDF_InitLibraryWithConfig(config);

  final destFilePath = stringToNativeInt8('sample.pdf');
  final srcFilePath = stringToNativeInt8('1417.pdf');

  final destDoc = pdfium.FPDF_LoadDocument(destFilePath, nullptr);
  if (destDoc == nullptr) {
    final err = pdfium.FPDF_GetLastError();
    throw PdfiumException.fromErrorCode(err);
  }

  final destDocPageCount = pdfium.FPDF_GetPageCount(destDoc);
  print('destDocPageCount: $destDocPageCount');

  final srcDoc = pdfium.FPDF_LoadDocument(srcFilePath, nullptr);
  if (srcDoc == nullptr) {
    final err = pdfium.FPDF_GetLastError();
    throw PdfiumException.fromErrorCode(err);
  }

  final srcDocPageCount = pdfium.FPDF_GetPageCount(srcDoc);
  print('srcDocPageCount: $srcDocPageCount');

  final ret = pdfium.FPDF_ImportPages(
      destDoc, srcDoc, stringToNativeInt8('5000'), 0, dll);
  print('FPDF_ImportPages: $ret');


  // free

  allocate.free(destFilePath);
  allocate.free(srcFilePath);

  pdfium.FPDF_CloseDocument(srcDoc);
  pdfium.FPDF_CloseDocument(destDoc);

  pdfium.FPDF_DestroyLibrary();
  allocate.free(config);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants