How to get Pdfium to work (done)

Just starting out? Need help? Post your questions and find answers here.
Hadrianus
User
User
Posts: 34
Joined: Wed Mar 27, 2013 11:31 pm

How to get Pdfium to work (done)

Post by Hadrianus »

See also this topic in general discussion (http://www.purebasic.fr/english/viewtop ... =7&t=65733).

Due to lack of help-files I use information from https://pdfium.patagames.com/help/html/ ... 3d257a.htm. Patagames use this library too, for their .net application. Only their pdfium.dll is different than the free one I downloaded from: https://github.com/pvginkel/PdfiumViewer

I tried this:

Code: Select all

Enumeration
  #PDFlib
EndEnumeration

Prototype InitLibrary(licencekey.s,path.s)
Prototype LoadDocuments(documentpath.s,password.s)
Prototype GetPageCounts(HandleToDocument.i)

If OpenLibrary(#PDFlib,"pdfium.dll")

  ExamineLibraryFunctions(#PDFlib)

  Debug IsLibrary(#PDFlib)  ; Just for testing. Value should be above zero.
  Debug LibraryID(#PDFlib)  ; Just for testing. Value should be above zero.
  Debug CountLibraryFunctions(#PDFlib)

  For i = 1 To CountLibraryFunctions(#PDFlib)

    Debug i
    NextLibraryFunction()
    Debug LibraryFunctionName()

  Next 
  

  Debug GetFunctionEntry(#PDFlib,128)                ; Just for testing. Value should be above zero.
  Debug GetFunction(#PDFlib,"_FPDF_InitLibrary@0")   ; Just for testing. Value should be above zero.

  Debug GetFunctionEntry(#PDFlib,131)                ; Just for testing. Value should be above zero.
  Debug GetFunction(#PDFlib,"_FPDF_LoadDocument@8")  ; Just for testing. Value should be above zero.
  
  Debug GetFunctionEntry(#PDFlib,122)                ; Just for testing. Value should be above zero.
  Debug GetFunction(#PDFlib,"_FPDF_GetPageCount@4")  ; Just for testing. Value should be above zero.  
  
  
  Init.InitLibrary = GetFunction(#PDFlib,"_FPDF_InitLibrary@0")  ; Call this function first before all other functions.
  Debug Init("",GetCurrentDirectory()+"pdfium.dll")

  LoadDocument.LoadDocuments = GetFunction(#PDFlib,"_FPDF_LoadDocument@8")
  Handle.i = LoadDocument(GetCurrentDirectory()+"name_of_document.pdf","")
  Debug Handle
  
  GetPageCount.GetPageCounts = GetFunction(#PDFlib,"_FPDF_GetPageCount@4")
  Debug GetPageCount(Handle)

  CloseLibrary(#PDFlib)

EndIf
Everything according to PB-manual, but no success. Perhaps an expert can have a look.
Last edited by Hadrianus on Fri Jun 10, 2016 7:54 pm, edited 1 time in total.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

This worked here:

Code: Select all

Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Prototype protoGetPageCount(HandleToDocument.i)


Procedure Main()
  
  Protected pdfdll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  Debug pdfdll
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdfdll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdfdll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdfdll,"_FPDF_LoadDocument@8")
  
  InitLibrary()
  
  Protected pdf = LoadDocument("mypdf2.pdf", "")
  
  Debug GetPageCount(pdf) 
  
  
  CloseLibrary(pdfdll)
  
EndProcedure

Main()
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

This works too ...

Code: Select all

Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags);As "_FPDF_RenderPage@32
Prototype protoLoadPage(document, page_index) ;"_FPDF_LoadPage@8"

Procedure Main(*win)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  
  InitLibrary()
  
  Protected pdf_doc = LoadDocument("mypdf2.pdf", "")
  
  Protected pdf_page = LoadPage(pdf_doc, 0)

   RenderPage(GetDC_(WindowID(*win)), pdf_page, 0, 0, WindowWidth(*win), WindowHeight(*win), 0, $100);

   Repeat
   
   Until WaitWindowEvent() = #PB_Event_CloseWindow
   
   CloseLibrary(pdf_dll)
  
EndProcedure


Main(OpenWindow(#PB_Any, 0, 0, 800, 640, "Pdfium", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget))
Hadrianus
User
User
Posts: 34
Joined: Wed Mar 27, 2013 11:31 pm

Re: How to get Pdfium to work

Post by Hadrianus »

Thanks a lot Acreis.

I guess my biggest mistake is not putting p-utf8 in documentpath (althrough I do not understand why it's necessary) and I did not use InitLibrary() as it should. Are you familiar with Pdfium and all the function-arguments?

I can continue my research now.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

Hi Hadrianus,

I'm very interested in pdf rendering, bud I don't know anything about pdfium.

I have done just some guesses.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work

Post by IdeasVacuum »

Certainly sounds interesting because it appears not to be using Ghost and it is covered by the very generous Apache Licenses v2.0. The file management looks a bit complicated.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: How to get Pdfium to work

Post by Mohawk70 »

acreis wrote:This works too ...

Code: Select all

Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags);As "_FPDF_RenderPage@32
Prototype protoLoadPage(document, page_index) ;"_FPDF_LoadPage@8"

Procedure Main(*win)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  
  InitLibrary()
  
  Protected pdf_doc = LoadDocument("mypdf2.pdf", "")
  
  Protected pdf_page = LoadPage(pdf_doc, 0)

   RenderPage(GetDC_(WindowID(*win)), pdf_page, 0, 0, WindowWidth(*win), WindowHeight(*win), 0, $100);

   Repeat
   
   Until WaitWindowEvent() = #PB_Event_CloseWindow
   
   CloseLibrary(pdf_dll)
  
EndProcedure


Main(OpenWindow(#PB_Any, 0, 0, 800, 640, "Pdfium", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget))
I downloaded from https://github.com/pvginkel/PdfiumViewer ( both 32 and 64 bit versions) and it doesn't work using either version of your code , with either dll. I get an error message stating ...
H:\DEV\pb\542\64\PDFium\x64\pdfium.dll is either not designed to run on Windows or it contains an error. Try installing the program againusing the original installation media or contact your system administrator or the software vendor for support. The only parts of your code I have modified is using the full path to the DLL and the pdf file I'm attempting to view. Nothing is wrong with the pdf as I can view it with all other viewers. Can you post the version of the dll you have somewhere that I can download it? I am using Win 7 64 , PB 5.42 64 and tried with both the 32 and 64 bit versions of the dll.
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

I downloaded it some minutes ago and it's working

The path is

Code: Select all

\PdfiumViewer-master\Libraries\Pdfium\x86\pdfium.dll
Hadrianus
User
User
Posts: 34
Joined: Wed Mar 27, 2013 11:31 pm

Re: How to get Pdfium to work

Post by Hadrianus »

I confirm it's working just fine. This library is no problem for PB !
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work

Post by IdeasVacuum »

Where are you getting the function parameters from? I'm particularly interested in these:

Code: Select all

Prototype protoGetFirstPageNum()
Prototype protoGetRotation()
Prototype protoGetText()
Prototype protoGetPageHeight()
Prototype protoGetPageWidth()

Protected GetFirstPageNum.protoGetFirstPageNum = GetFunction(pdf_dll, "_FPDFAvail_GetFirstPageNum@4")
Protected GetRotation.protoGetRotation = GetFunction(pdf_dll, "_FPDFPage_GetRotation@4")
Protected GetText.protoGetText = GetFunction(pdf_dll, "_FPDFText_GetText@16")
Protected GetPageHeight.protoGetPageHeight = GetFunction(pdf_dll, "_FPDF_GetPageHeight@4")
Protected GetPageWidth.protoGetPageWidth = GetFunction(pdf_dll, "_FPDF_GetPageWidth@4")
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Hadrianus
User
User
Posts: 34
Joined: Wed Mar 27, 2013 11:31 pm

Re: How to get Pdfium to work

Post by Hadrianus »

I get them from: https://pdfium.patagames.com/help/html/ ... 3d257a.htm

Be aware that their commercial dll seems to be bigger than the free one from https://github.com/pvginkel/PdfiumViewer

This free dll, made from open source google/foxit with Apache2 license, has less functions inside, but still enough in order to make something nice.

By the way: if you want to do something with zooming and moving the pdf, you need the function GetMediaBox and not GetPageWidth and GetPageHeight.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work

Post by IdeasVacuum »

Thanks Hadrianus. I wasn't thinking of zooming the page (though no doubt useful), but to render it onto a canvas instead of the client area of the window. The canvas is on a ScrollAreaGadget(). No luck so far but that is because I don't know what I'm doing:

Code: Select all

Procedure PfPdfPage(iGadget, iPageNum.i)
;#--------------------------------------
Protected pdf_page = LoadPage(igPdfDoc, iPageNum)
Protected       dc = CreateCompatibleDC_(GadgetID(iGadget))

              RenderPage(dc, pdf_page, 0, 0, igWpix, igHpix, 0, $100)
EndProcedure
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

Hi Ideas

Please post a minimum working or not working program so we can help you with little effort.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work

Post by IdeasVacuum »

Hello acreis

I put a snippet there but since I do not know how to retrieve a handle to a device context for a CanvasGadget, any amount of sample code from me will not make matters better :D

I tried

Code: Select all

dc = CreateCompatibleDC_(GadgetID(#MyCanvas))

Code: Select all

dc = GetDC_(GadgetID(#MyCanvas))
I have also tried other examples on the forum, but they are not really similar to this requirement.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work

Post by acreis »

Hi Ideas,

I'll try investigate the issue as soon as possible.

The smallest compilable non working code would help.

Please try

Code: Select all

hdc = StartDrawing(CanvasOutput(#canvas))
Post Reply