Skip to main content

bugs - Unable to communicate with LinkObject when importing PDF


Bug introduced in 8 or earlier and persisting through 11.1




When I try to download some files from the web, I sometimes get an error like this:


links = {"http://dip21.bundestag.de/dip21/btd/18/076/1807620.pdf",

"http://epaper.das-parlament.de/epaper/ausgabe.pdf",
"http://gemeindebund.at/images/uploads/downloads/2014/Publikationen/RFGs/2010/RFG_4-2010_-_E-Government_in_Gemeinden_(PDF__2_4MB).pdf",
"http://smart-government.eu/wp-content/uploads/2013/02/PB_IRML-SmartSchool.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/113.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/163.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/51.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/P-261.pdf",
"https://www.bundes-sgk.de/system/files/documents/impulse2_nov2015_final.pdf",
"https://www.demo-online.de/system/files/demo_05_06_ansicht_pdf_klein.pdf",
"https://www.gruene-bw.de/app/uploads/2016/05/GrueneBW-Koalitionsvertrag-2016-Entwurf.pdf",

"https://www.itdz-berlin.de/dokumente/splitter/splitter_2005_1.pdf",
"https://www.normenkontrollrat.bund.de/Webs/NKR/Content/DE/Publikationen/Jahresberichte/2014-10-01_nkr_jahresbericht_2014.pdf?__blob=publicationFile&v=1",
"http://www.beamten-informationen.de/media/pdf/Beamten_Magazin_2008_04.pdf",
"http://www.dgb.de/themen/++co++541b3e54-263c-11e5-9b43-52540023ef1a",
"http://www.staedtetag-nrw.de/imperia/md/content/stnrw/internet/2_fachinformationen/2008/ag_4_geoportale_formatiert_korr_kes.pdf",
"http://www.vitako.de/Themen%20Dokumente/Vitako_aktuell_01-16.pdf"};
res = Import[#, "Plaintext"] & /@ links

LinkObject::linkd: Unable to communicate with closed link LinkObject["C:\Program Files\Wolfram Research\Mathematica\11.0\SystemFiles\Converters\Binaries\Windows-x86-64\PDF.exe",2011,5].


What does this error mean and how can I cleanly handle it (without keeping a PDF.exe in memory)?



Answer



Just for reference, this is my workaround:


importPatched[url_, elements_String] := importPatched[url, {elements}];
importPatched[url_List, elements_List] := Dataset[importPatched[#, elements] & /@ url];
importPatched[url_String, elements_List] := Module[{fn, pdftohtml, dlResult, pdfQ, s, elems, result, evData, i},
pdftohtml = "pdftohtml.exe"; (* edit path if necessary *)
fn = CreateFile[];
result = Prepend[Association[Rule[#, Missing[]] & /@ elements], Rule["Link", url]];


(* download *)
evData = EvaluationData[URLDownload[url, fn, {"StatusCode", "ContentType"}]];
If[Not[evData["Success"]], Return[Association[Prepend["Status" -> "URLDownload Error " <> StringRiffle[ToString /@ evData["Messages"], ", "], result]]]];
dlResult = evData["Result"];
If[dlResult["StatusCode"] =!= 200,
Quiet[DeleteFile[fn]];
Return[Prepend[result, "Status" -> "HTTP Error " <> dlResult["StatusCode"]]];
];

(* convert PDF to html *)

pdfQ = AnyTrue[{"application/acrobat", "application/pdf", "application/vnd.pdf", "application/x-pdf", "text/pdf", "text/x-pdf"},
StringContainsQ[dlResult["ContentType"], Verbatim[#], IgnoreCase -> True]& ];
s = If[pdfQ,
RenameFile[fn, fn <> ".pdf"];
fn = fn <> ".pdf";
evData = EvaluationData[StringJoin@ReadList["!" <> pdftohtml <> " -stdout " <> fn, Character]];
If[Not[evData["Success"]], Return[Prepend[result, "Status" -> "pdftohtml Error"]]];
evData["Result"]
, (* else *)
ReadString[fn]

];
Quiet[DeleteFile[fn]];

(* read requested elements *)
elems = Intersection[elements, ImportString[s, "Elements"]];
i = 1;
While[i <= Length[elems],
evData = EvaluationData[ImportString[s, elems[[i]]]];
If[Not[evData["Success"]], Return[Prepend[result, "Status" -> "Import Error " <> StringRiffle[ToString /@ evData["Messages"], ", "]]]];
result[elems[[i]]] = evData["Result"];

i++
];
Return[Prepend[result, "Status" -> "Success"]];
];
ans = importPatched[links, {"Title", "Hyperlinks"}]

It produces more or less reasonable results on all links above. It relies on the great pdftohtml tool. The function basically downloads the link, looks if it is a pdf (in which case the file is transformed to html), and then calls Import.


For calling pdftohtml I asked a separate question and got help quickly.


Any hints on bugs or possible improvements on this code very welcome! In particular, it would be interesting to know how to include the pdftohtml.exe in a subdirectory of a Mathematica package.


Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...