react-native-pdf-lib icon indicating copy to clipboard operation
react-native-pdf-lib copied to clipboard

PDFLib.getDocumentsDirectory() of undefined after Linking

Open danielezra opened this issue 5 years ago • 12 comments

Hello, I keep getting an PDFLib.getDocumentsDirectory() of undefined error even though I followed all the steps on the installation. I would really appreciate it if someone could help enlighten me with this error.

I'm using Cocoapods and I even tried to link the dependencies and also tried doing those steps alone but no luck.

Please help.

React Native Version: 0.61.2 react-native-pdf-lib: 0.2.0 and 0.2.1 (Tried both but are not working for me)

danielezra avatar Nov 14 '19 07:11 danielezra

Could you supply an example project so I can reproduce and get to the bottom of it?

thekevinbrown avatar Nov 14 '19 09:11 thekevinbrown

Thanks for the response @thekevinbrown, here's the reproduction

https://github.com/danielezra/IssueReproduction

Steps taken

  1. Fresh react native project
  2. Install react-native-pdf-lib
  3. Linked through cocoa pods and react-native-link

Tried doing react-native-link alone and pod install alone but still getting the error.

image

danielezra avatar Nov 14 '19 14:11 danielezra

Any finding on this @thekevinbrown?

danielezra avatar Nov 19 '19 05:11 danielezra

For what its worth, I have run into this same issue. I'm going to try to dig a little further and will update if I figure something out.

jdkrebs avatar Nov 19 '19 20:11 jdkrebs

For what its worth, I have run into this same issue. I'm going to try to dig a little further and will update if I figure something out.

I don't know much about pods yet but got it working. The problem is that the pdflib library does not get compiled because the source is not there, and secondly the static inlcuded libraries don't get linked. I got around it this way:

  1. Install latest version from github: npm install --save https://github.com/Hopding/react-native-pdf-lib
  2. Edit RNReactNativePdfLib.podspec in node_modules/react-native-pdf-lib/ios/: 2.1. Include the source files that end in .mm in stead of .m: s.source_files = "*.{h,mm}" 2.2. Add the static libraries that are included: s.ios.vendored_libraries =["lib/universal/libFreeType.a", "lib/universal/libLibJpeg.a", "lib/universal/libLibTiff.a", "lib/universal/libPDFWriter.a", "lib/universal/libZlib.a"].
  3. in ios/ run pod install
  4. Copy the header files. From node_modules/react-native-pdf-lib/ios/lib/include/ to ios/Pods/Headers/Private/RnReactNativePdfLib/. Also copy the header files from subdirs freetype/ and freetype/config/
  5. Now RnReactNativePdfLib library in pods project should compile and the libs link succesffully in your project.

zilicon avatar Dec 11 '19 16:12 zilicon

One of the things I'm hoping to do in the future is move away from PDFLib and to just using the built in CG PDF rendering methods on iOS, so that should help this as well.

thekevinbrown avatar Dec 12 '19 05:12 thekevinbrown

@zilicon any chance you could make a PR for this so I can roll it into the library?

thekevinbrown avatar Feb 09 '20 23:02 thekevinbrown

@zilicon I created a fork using your fix, and I added/change to do the import automatically too https://github.com/jdaoliveira/react-native-pdf-lib

I tested as best as I could, but as I am in work I can't fully test.

ghost avatar Feb 12 '20 11:02 ghost

@jdaoliveira, could you make a PR for these changes?

thekevinbrown avatar Feb 26 '20 03:02 thekevinbrown

@thekevinbrown Done, to make this work i had to remove the folder, probably not the best way, but i couldn't find a way to do it with folders.

ghost avatar Feb 26 '20 07:02 ghost

Based on the fix found by @zilicon, I made a modification to the podspec which resolve the issue.

TL,DR: add this to the podspec

  s.header_mappings_dir = "lib/include"

Explication: The resulting podspec is now :

require 'json'

package = JSON.parse(File.read(File.join(__dir__, '../package.json')))

Pod::Spec.new do |s|
  s.name                = "RNReactNativePdfLib"
  s.version             = package['version']
  s.summary             = package['description']
  s.description         = package['description']
  s.homepage            = package['homepage']
  s.license             = package['license']
  s.author              = package['author']
  s.source              = { :git => 'https://github.com/Hopding/react-native-pdf-lib.git', :tag => 'v'+s.version.to_s }

  s.platform              = :ios, '9.0'
  s.ios.deployment_target = '8.0'
  s.dependency 'React'
  s.source_files  = "**/*.{h,mm}"
  s.header_mappings_dir = "lib/include"
  s.ios.vendored_libraries =["lib/universal/libFreeType.a", "lib/universal/libLibJpeg.a", "lib/universal/libLibTiff.a", "lib/universal/libPDFWriter.a", "lib/universal/libZlib.a"]
end

After the modification given by @zilicon, the remaining issue is linked to unresolved header import. This is caused by cocoapods which flatten the headers directories. This line prevents this side-effect :

  s.header_mappings_dir = "lib/include"

@jdaoliveira, I think you could adapt you're PR with this.

balzdur avatar Mar 10 '20 16:03 balzdur

as this issue is still not fixed and there is a font issue now appearing on rn 0.63I forked the fix for that to add on @balzdur PR. If you can't wait you can for the merge to happen, replace whit this line in your package.json and you should be worry free on ios: "react-native-pdf-lib": "github:zilicon/react-native-pdf-lib#fix-fonts",

zilicon avatar Jan 08 '21 16:01 zilicon