I want to know how to display annotations when using the libvips copy command to image PDFs.

Asked 1 years ago, Updated 1 years ago, 301 views

What do you want to do

  • I would like to see an annotation when I use the libvips vips copy command to image a PDF.
  • I want to use pdfium for render.

Tried Commands

  • Install libvips
export PREFIX=/usr/local/vips
export PKG_CONFIG_PATH = $PREFIX/lib/pkgconfig
exportLD_LIBRARY_PATH = $PREFIX/lib
export PATH=$PATH:$PREFIX/bin

WORKDIR=/usr/local/src
VIPS_URL=https://github.com/libvips/libvips/releases/download
PDFIUM_URL=https://github.com/bblanchon/pdfium-binaries/releases/download/chromium

PDFIUM_VERSION=5351
VIPS_VERSION= 8.13.2

# Install Related Libraries
yum install-y\
    libpng-develop\
    poppler-glib-develop\
    glib2-develop\
    libjpeg-develop\
    expat-develop\
    zlib-develop\
    orc-develop\
    lcms2-develop\
    libexif-develop\
    libgsf-develop

# pdftoppm
yum install-y poppler-utils

cd$WORKDIR

## install PDFIUM
mkdir pdfium-$PDFIUM_VERSION\
  &cdpdfium-$PDFIUM_VERSION\
  &wget$PDFIUM_URL/$PDFIUM_VERSION/pdfium-linux-x64.tgz
mkdir-p$PREFIX/lib/pkgconfig\
  &cd$PREFIX\
  &tar xf$WORKDIR/pdfium-$PDFIUM_VERSION/pdfium-linux.tgz\
  &echo "prefix=$PREFIX">lib/pkgconfig/pdfium.pc\
  &echo "exec_prefix=\${prefix}">lib/pkgconfig/pdfium.pc\
  &echo "libdir=\${exec_prefix}/lib">lib/pkgconfig/pdfium.pc\
  &echo "includedir=\${prefix}/include">lib/pkgconfig/pdfium.pc\
  &echo "Name:pdfium" > lib/pkgconfig/pdfium.pc\
  &echo "Description: pdfium" > lib/pkgconfig/pdfium.pc\
  &echo "Version: $PDFIUM_VERSION" > lib/pkgconfig/pdfium.pc\
  &echo "Requires:">lib/pkgconfig/pdfium.pc\
  &echo "Libs:-L\${libdir}-lpdfium">lib/pkgconfig/pdfium.pc\
  &echo "Cflags:-I\${includedir}">lib/pkgconfig/pdfium.pc

## install libvips
wget$VIPS_URL/v$VIPS_VERSION/vips-$VIPS_VERSION.tar.gz\
  &tar xf vips-$VIPS_VERSION.tar.gz\
  &cd vips-$VIPS_VERSION\
  &./configure -- prefix$PREFIX\
  &make V = 0\
  & make install
  • Image PDF
vips copy input.pdf [page=1,dpi=192] output_1_.jpg [Q=100]

Tried

  • PDF contents saved as jpeg image.(OK)
  • However, the annotation that exists in the PDF was not shown in the jpeg image.(NG)

Annotated when converted with ghostscript
No annotations when converted to libvips

pdf

2023-01-08 05:34

1 Answers

You can compile libvips to view annotations when you image PDFs as follows:
Add the fourth line (sed-z~).

wget$VIPS_URL/v$VIPS_VERSION/vips-$VIPS_VERSION.tar.gz\
  &tar xf vips-$VIPS_VERSION.tar.gz\
  &cd vips-$VIPS_VERSION\
  &sed-z-i's/FPDF_RenderPageBitmap[^;]*;/FPDF_RenderPageBitmap(bitmap, pdf->page, 0, rect.width, rect.height, 0, 1);/'/libvips/foreign/pdfiumload.c\
  &./configure -- prefix$PREFIX\
  &make V = 0\
  & make install

libvips specifies the option not to display annotations when running the FPDF_RenderPageBitmap function in PDFium. Above, the source code has been rewritten and compiled with the option to view annotations.

If you run the last argument of the FPDF_RenderPageBitmap function with RenderFlags.FPDF_ANNOT=1, you will see PDF annotations.

public static void FPDF_RenderPageBitmap(
    IntPtr bitmap,
    IntPtr page,
    int start_x,
    int start_y,
    int size_x,
    int size_y,
    PageRotate rotation,
    RenderFlags flags
)

https://pdfium.patagames.com/help/html/M_Patagames_Pdf_Pdfium_FPDF_RenderPageBitmap.htm

If you look at the libvips side code, the last argument is 0 and you specify the option not to display annotations. You can achieve your goal by changing the last argument to 1.

libvips/foreign/pdfiumload.c

FPDF_RenderPageBitmap(bitmap, pdf->page, 
    0,0, rect.width, rect.height,
    0, 0 ); 

https://github.com/libvips/libvips/blob/976db37f84b5ab2d77e85cf3b123f63f97c35d39/libvips/foreign/pdfiumload.c#L592

sed-z-i's/FPDF_RenderPageBitmap[^;]*;/FPDF_RenderPageBitmap(bitmap, pdf->page,0,0,rect.width,rect.height,0,1);/'/libvips/foreign/pdfiumload.c 

The command above changes the last argument to 0→1 as follows:

FPDF_RenderPageBitmap(bitmap, pdf->page, 
    0,0, rect.width, rect.height,
    0, 0 ); 
       ↓
FPDF_RenderPageBitmap (bitmap, pdf-> page, 0, 0, rect.width, rect.height, 0, 1);
        


2023-01-08 08:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.